Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么选择Thread.CurrentContext属性和Thread.GetDomain()方法?_C#_Multithreading_Appdomain_Threadcontext - Fatal编程技术网

C# 为什么选择Thread.CurrentContext属性和Thread.GetDomain()方法?

C# 为什么选择Thread.CurrentContext属性和Thread.GetDomain()方法?,c#,multithreading,appdomain,threadcontext,C#,Multithreading,Appdomain,Threadcontext,这不是一个非常重要的问题,但我想知道为什么Thread类公开了一个用于获取当前上下文的属性(Thread.CurrentContext)和一个用于获取当前AppDomain的方法(Thread.GetDomain()) 了解Process>AppDomain>Context>Thread的层次结构,我的假设是线程的上下文在当前时间点已知,并且需要根据当前上下文搜索域 但我想听到更明智的答案。谢谢 我的假设是线程的上下文在当前已知 时间点,并且需要基于当前 上下文 事实上,在.NET Framew

这不是一个非常重要的问题,但我想知道为什么Thread类公开了一个用于获取当前上下文的属性(Thread.CurrentContext)和一个用于获取当前AppDomain的方法(Thread.GetDomain()

了解Process>AppDomain>Context>Thread的层次结构,我的假设是线程的上下文在当前时间点已知,并且需要根据当前上下文搜索域

但我想听到更明智的答案。谢谢

我的假设是线程的上下文在当前已知 时间点,并且需要基于当前 上下文

事实上,在.NET Framework的当前实现中,
上下文
对象指向其父域。框架设计者可能已经将上下文的域公开为
Thread.context.domain
。这可能是一个反问为什么他们没有这样做;我无法通过查看参考源代码来判断这一点

重要的是,在任何给定时刻,线程都在特定域内执行代码。这可能是进程的默认域,也可能是通过
AppDomain.DoCallBack
AppDomain.ExecuteAssembly
或封送的
MarshallByRefObject
-对象输入的域那将是域
Thread.GetDomain()
返回。

此域至少有一个上下文(默认上下文),但也可能有为
ContextBoundObject
-对象创建的其他上下文。可以通过
Context.DoCallBack
在同一域上显式输入这些上下文中的任何一个,也可以通过调用编组的
ContextBoundObject
-对象从任何域隐式输入这些上下文这就是上下文
线程。上下文
返回

线程和域或线程和上下文之间没有父子关系。然而,域及其上下文之间存在严格的父子、一对多关系因此,不需要根据当前上下文搜索域。

如果你想玩得更多,下面是我使用的应用程序:

using System;
using System.Runtime.Remoting.Contexts;
using System.Threading;

namespace ConsoleApplication
{
    public class Program
    {
        [Synchronization]
        public class CtxObject : ContextBoundObject
        {
            public void Report(string step)
            {
                Program.Report(step);
            }
        }

        public static void Main(string[] args)
        {
            Program.Report("app start");
            System.AppDomain domain = System.AppDomain.CreateDomain("New domain");

            var ctxOb = new CtxObject();
            ctxOb.Report("ctxOb object");

            domain.SetData("ctxOb", ctxOb);
            domain.DoCallBack(() => 
            {
                Program.Report("inside another domain");
                var ctxOb2 = (CtxObject)System.AppDomain.CurrentDomain.GetData("ctxOb");
                ctxOb2.Report("ctxOb called from another domain");
            });

            Console.ReadLine();
        }

        static void Report(string step)
        {
            var threadDomain = Thread.GetDomain().FriendlyName;
            Console.WriteLine(
                new
                {
                    // Thread.CurrentContext.ContextID is only unique for the scope of domain
                    step,
                    ctx = Thread.CurrentContext.GetHashCode(),
                    threadId = Thread.CurrentThread.ManagedThreadId,
                    domain = Thread.GetDomain().FriendlyName,
                });
        }
    }
}

我很确定你对等级制度的理解是错误的。线程观察上下文。
thread
context
是独立的实体。
Context
ContextBoundObject
对象相关,而不是线程。多个上下文可以流经同一线程(使用
Context.DoCallBack
),多个线程可以共享同一上下文(
thread.CurrentContext
)。您不能将线程“移动”到另一个域。相关:。@当然你可以将一个线程“移动”到另一个域。来自Andrew Troelsen的Pro C#5.0和.NET 4.5框架:“当Windows OS线程调度程序和.NET CLR认为合适时,线程可以自由跨越应用程序域边界。”@Gusdor关于线程上下文关系,同样来自Andrew Troelsen的Pro C#5.0和.NET 4.5框架:“单个线程也可能在任何给定时间被移动到特定上下文中,并且可能在CLR的突发奇想下被重新定位到新上下文中。”因此,我对层次结构的理解非常准确。只要使用一个合适的反编译程序,就可以看到这些成员在.NET Framework本身中被大量使用。GetDomain()最明显的用途是是AppDomain.CurrentDomain,它们都在远程处理胶水中大量使用。