|
using System.Threading;
using System;
namespace ThreadBound
{
/// <summary>
/// Synchronization context for the worker thread context. If a class is bound to a thread via the
/// [ThreadBound]-attribute the class will be executed within its own worker thread. This class
/// implements the synchronization context for the Worker class. It will be automatically created
/// if a Worker instance is created.
/// </summary>
internal class WorkerSynchronizationContext :
SynchronizationContext,
IDisposable
{
/// <summary>
/// Reference to the worker that is used for executing the methods.
/// </summary>
private Worker AssignedWorker;
/// <summary>
/// C'tor
/// </summary>
/// <param name="workerToUse">Reference to the worker that will execute the callbacks passed to this synchronization context.</param>
public WorkerSynchronizationContext(Worker workerToUse)
{
AssignedWorker = workerToUse;
}
/// <summary>
/// Overrides the Post method in SynchronizationContext.
/// </summary>
/// <see cref="SynchronizationContext.Post"/>
public override void Post(SendOrPostCallback d, object state)
{
AssignedWorker.EnqueAsync(d, state);
}
/// <summary>
/// Overrides the Send method in SynchronizationContext.
/// </summary>
/// <param name="d"></param>
/// <see cref="SynchronizationContext.Send"/>
public override void Send(SendOrPostCallback d, object state)
{
AssignedWorker.EnqueSync(d, state);
}
/// <summary>
/// Overrides the CreateCopy of the SynchronizationContext.
/// </summary>
/// <see cref="SynchronizationContext.SynchronizationContext"/>
public override SynchronizationContext CreateCopy()
{
return new WorkerSynchronizationContext(AssignedWorker);
}
/// <summary>
/// Wird der Synchronisationskontext abgebaut, so wird der zugewiesene Worker angehalten.
/// </summary>
public void Dispose()
{
AssignedWorker.Stop();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.