这不是一个非常重要的问题,但我想知道为什么Thread类会公开一个用于获取当前上下文(Thread.CurrentContext)的属性和一个用于获取当前AppDomain(Thread.GetDomain())的方法。
了解进程(Process) > 应用程序域(AppDomain) > 上下文(Context) > 线程(Thread)的层次结构,我的假设是线程的上下文在当前时间点已知,需要根据当前上下文搜索应用程序域。
但我想听到更明智的答案。谢谢!
这不是一个非常重要的问题,但我想知道为什么Thread类会公开一个用于获取当前上下文(Thread.CurrentContext)的属性和一个用于获取当前AppDomain(Thread.GetDomain())的方法。
了解进程(Process) > 应用程序域(AppDomain) > 上下文(Context) > 线程(Thread)的层次结构,我的假设是线程的上下文在当前时间点已知,需要根据当前上下文搜索应用程序域。
但我想听到更明智的答案。谢谢!
我的假设是,当前上下文中已知线程的上下文,并且需要基于当前上下文搜索域。
实际上,在.NET Framework的当前实现中,Context
对象保留对其父域的引用。框架设计人员可能已将上下文的域公开为Thread.Context.Domain
。很可能会有一个反问句,即为什么他们没有这样做;通过查看参考源代码,我无法得知。
重要的是,在任何给定时间点,线程都在特定域内执行代码。这将是进程的默认域,或者通过AppDomain.DoCallBack
、AppDomain.ExecuteAssembly
或一个已封送的MarshalByRefObject
对象进入的域。这将是Thread.GetDomain()
返回的域。
ContextBoundObject
对象创建的上下文。可以通过Context.DoCallBack
显式地进入同一域中的任何上下文,或者通过调用已编组的ContextBoundObject
对象从任何域中隐式进入。 这就是Thread.Context
返回的上下文。
线程与域或上下文之间没有父子关系。但是,域和其上下文之间存在严格的父子关系。 因此,不需要基于当前上下文搜索域。
如果您想更多地玩一下,这是我使用的应用程序: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
)。但是您无法将线程“移动”到另一个域中。相关链接:https://dev59.com/VX3aa4cB1Zd3GeqPfqCs。 - noseratio - open to work