I am trying to discover how the ExecutionContext actually works in version 4.0 and above of the .NET Framework. The documentation says that the managed principle, synchronization, locale and user context all flow to the new thread when using Thread.Start and most thread pool operations. But I cannot see this working at all in practice.
Here is a simple console application that tests if the synchronization context and managed principle flow when starting a new thread...
static void Main(string[] args)
{
SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("One"), null);
Thread t1 = new Thread(new ThreadStart(ThreadRun));
t1.Start();
t1.Join();
SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("Two"), null);
AsyncFlowControl aFC = ExecutionContext.SuppressFlow();
Thread t2 = new Thread(new ThreadStart(ThreadRun));
t2.Start();
t2.Join();
aFC.Undo();
Console.Read();
}
static void ThreadRun()
{
Console.WriteLine("ThreadRun Id={0} Context={1} Principle={2}",
Thread.CurrentThread.ManagedThreadId,
(SynchronizationContext.Current != null),
Thread.CurrentPrincipal.Identity.Name);
}
The result is...
ThreadRun Id=11 Context=False Principle=One
ThreadRun Id=12 Context=False Principle=Two
So the synchronization context never flows and the managed principle always flows even when you specify it should not. Basically the documentation is completely wrong. So is there a description of what ExecutionContext does in reality and why it is useful?