如何.NET执行上下文的实际工作? [英] How does .NET ExecutionContext actually work?

查看:234
本文介绍了如何.NET执行上下文的实际工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发现<如何href="http://msdn.microsoft.com/en-us/library/system.threading.executioncontext%28v=vs.100%29.aspx">ExecutionContext实际工作的.NET Framework 4.0及以上版本。该文件说,管理的原则,同步,区域设置和用户上下文全部流向新线程时使用Thread.Start最线程池操作。但我看不到这方面的工作都在实践。

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

所以,同步方面从来没有流和管理的原则,当你指定它不应该总是流动均匀。基本的文档是完全错误的。那么,有什么样的ExecutionContext确实在现实中的描述,以及为什么它是有用的?

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?

推荐答案

这是pretty的误导性的文件。我不能回答你的问题的更广泛的推力,但我可以告诉你为什么的SynchronizationContext 简化版,流。

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.

如果你看一下 Thread.Start ,它最终调用到源代码:

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);
    }

请注意,它明确地传递 ExecutionContext.CaptureOptions.IgnoreSyncCtx 默认情况下。它还传递 CallContext.Principal 无论ExecutionContext.Sup pressFlow()。因此,解释了为什么你看到你所看到的,而不是在它可能是有用的或为什么文档是平出错误的!

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!

这篇关于如何.NET执行上下文的实际工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆