Question

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?

Was it helpful?

Solution

That's pretty misleading documentation. I can't answer the broader thrust of your question, but I can tell you why SynchronizationContext does't flow.

If you look at the source of Thread.Start, it eventually calls down to:

    [SecuritySafeCritical]
    private void Start(ref StackCrawlMark stackMark)
    {
      this.StartupSetApartmentStateInternal();
      if (this.m_Delegate != null)
        ((ThreadHelper) this.m_Delegate.Target).SetExecutionContextHelper(ExecutionContext.Capture(ref stackMark, ExecutionContext.CaptureOptions.IgnoreSyncCtx));
      this.StartInternal(CallContext.Principal, ref stackMark);
    }

Note that it explicitly passes ExecutionContext.CaptureOptions.IgnoreSyncCtx by default. It also passes CallContext.Principal regardless of ExecutionContext.SuppressFlow(). So, the explains why you are seeing what you are seeing, but not when it might be useful or why the docs are flat out wrong!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top