Unosquare
    Show / Hide Table of Contents

    Class WorkerBase

    Provides base infrastructure for Timer and Thread workers.

    Inheritance
    Object
    WorkerBase
    ThreadWorkerBase
    TimerWorkerBase
    Implements
    IWorker
    IDisposable
    Inherited Members
    Object.ToString()
    Object.Equals(Object)
    Object.Equals(Object, Object)
    Object.ReferenceEquals(Object, Object)
    Object.GetHashCode()
    Object.GetType()
    Object.MemberwiseClone()
    Namespace: Swan.Threading
    Syntax
    public abstract class WorkerBase : IWorker, IDisposable

    Constructors

    WorkerBase(String, TimeSpan)

    Initializes a new instance of the WorkerBase class.

    Declaration
    protected WorkerBase(string name, TimeSpan period)
    Parameters
    Type Name Description
    String name

    The name.

    TimeSpan period

    The execution interval.

    Properties

    CycleCancellation

    Gets the cycle logic cancellation owner.

    Declaration
    protected CancellationTokenOwner CycleCancellation { get; }
    Property Value
    Type Description
    CancellationTokenOwner

    CycleCompletedEvent

    Gets the cycle completed event.

    Declaration
    protected ManualResetEventSlim CycleCompletedEvent { get; }
    Property Value
    Type Description
    ManualResetEventSlim

    CycleStopwatch

    Gets the cycle stopwatch.

    Declaration
    protected Stopwatch CycleStopwatch { get; }
    Property Value
    Type Description
    Stopwatch

    DefaultPeriod

    Gets the default period of 15 milliseconds which is the default precision for timers.

    Declaration
    protected static TimeSpan DefaultPeriod { get; }
    Property Value
    Type Description
    TimeSpan

    IsDisposed

    Gets a value indicating whether this instance is disposed.

    Declaration
    public bool IsDisposed { get; protected set; }
    Property Value
    Type Description
    Boolean

    true if this instance is disposed; otherwise, false.

    IsDisposing

    Gets a value indicating whether this instance is currently being disposed.

    Declaration
    public bool IsDisposing { get; protected set; }
    Property Value
    Type Description
    Boolean

    true if this instance is disposing; otherwise, false.

    IsStopRequested

    Gets a value indicating whether stop has been requested. This is useful to prevent more requests from being issued.

    Declaration
    protected bool IsStopRequested { get; }
    Property Value
    Type Description
    Boolean

    Name

    Gets the name identifier of this worker.

    Declaration
    public string Name { get; }
    Property Value
    Type Description
    String

    Period

    Declaration
    public TimeSpan Period { get; set; }
    Property Value
    Type Description
    TimeSpan

    StateChangedEvent

    Gets the state changed event.

    Declaration
    protected ManualResetEventSlim StateChangedEvent { get; }
    Property Value
    Type Description
    ManualResetEventSlim

    StateChangeRequests

    Gets the state change requests.

    Declaration
    protected Dictionary<WorkerBase.StateChangeRequest, bool> StateChangeRequests { get; }
    Property Value
    Type Description
    Dictionary<WorkerBase.StateChangeRequest, Boolean>

    StateChangeTask

    Gets or sets the state change task.

    Declaration
    protected Task<WorkerState> StateChangeTask { get; set; }
    Property Value
    Type Description
    Task<WorkerState>

    WorkerState

    Gets the current state of the worker.

    Declaration
    public WorkerState WorkerState { get; protected set; }
    Property Value
    Type Description
    WorkerState

    Methods

    ComputeCycleDelay(WorkerState)

    Computes the cycle delay.

    Declaration
    protected int ComputeCycleDelay(WorkerState initialWorkerState)
    Parameters
    Type Name Description
    WorkerState initialWorkerState

    Initial state of the worker.

    Returns
    Type Description
    Int32

    The number of milliseconds to delay for.

    Dispose()

    Declaration
    public void Dispose()

    Dispose(Boolean)

    Releases unmanaged and - optionally - managed resources.

    Declaration
    protected virtual void Dispose(bool disposing)
    Parameters
    Type Name Description
    Boolean disposing

    true to release both managed and unmanaged resources; false to release only unmanaged resources.

    ExecuteCycleLogic(CancellationToken)

    Represents the user defined logic to be executed on a single worker cycle. Check the cancellation token continuously if you need responsive interrupts.

    Declaration
    protected abstract void ExecuteCycleLogic(CancellationToken cancellationToken)
    Parameters
    Type Name Description
    CancellationToken cancellationToken

    The cancellation token.

    OnCycleException(Exception)

    Handles the cycle logic exceptions.

    Declaration
    protected abstract void OnCycleException(Exception ex)
    Parameters
    Type Name Description
    Exception ex

    The exception that was thrown.

    OnDisposing()

    This method is called automatically when Dispose() is called. Makes sure you release all resources within this call.

    Declaration
    protected abstract void OnDisposing()

    OnStateChangeProcessed(WorkerState, WorkerState)

    Called when a state change request is processed.

    Declaration
    protected virtual void OnStateChangeProcessed(WorkerState previousState, WorkerState newState)
    Parameters
    Type Name Description
    WorkerState previousState

    The state before the change.

    WorkerState newState

    The new state.

    PauseAsync()

    Declaration
    public abstract Task<WorkerState> PauseAsync()
    Returns
    Type Description
    Task<WorkerState>

    ResumeAsync()

    Declaration
    public abstract Task<WorkerState> ResumeAsync()
    Returns
    Type Description
    Task<WorkerState>

    StartAsync()

    Declaration
    public abstract Task<WorkerState> StartAsync()
    Returns
    Type Description
    Task<WorkerState>

    StopAsync()

    Declaration
    public abstract Task<WorkerState> StopAsync()
    Returns
    Type Description
    Task<WorkerState>

    Implements

    IWorker
    System.IDisposable

    See Also

    IWorker

    Comments

    Back to top Copyright © 2017-2019 Unosquare