How does .NET ExecutionContext actually work?

浪尽此生 提交于 2019-12-10 03:54:03

问题


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?


回答1:


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!



来源:https://stackoverflow.com/questions/9815575/how-does-net-executioncontext-actually-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!