I've been investigating SynchronizationContext and these articles (Understanding SynchronizationContext, ExecutionContext vs SynchronizationContext) really helped me along, but - of course - many questions remain.
I would like to implement my own SynchronizationContext
class that inherits from the base class so that I can use it in the context of any Task
(for custom message loops, throttling, tracking etc.); something like this:
public class SyncContext : System.Threading.SynchronizationContext
{
public SyncContext() : base()
{
}
public override void Send(Threading.SendOrPostCallback d, object state)
{
base.Send(d, state);
}
public override void Post(Threading.SendOrPostCallback d, object state)
{
base.Post(d, state);
}
}
While base.Send
appears to do what expected (execute the callback synchronously), base.Post
doesn't appear to do anything. Regarding the Post
method Microsoft states:
When overridden in a derived class, dispatches an asynchronous message to a synchronization context.
I guess the base class can't be blamed for not having an async mechanism implemented, but what should happen when base.Post
is called or how could a correct/better implementation look? Is my general approach already misleading?
Thanks for your efforts!
A follow-up question: Would implementing a custom TaskScheduler
maybe be a better approach - maybe TaskScheduler that uses a dedicated thread is essentially what I need?