为什么 Thread.CurrentContext 属性和 Thread.GetDomain() 方法? [英] Why Thread.CurrentContext property and Thread.GetDomain() method?

查看:20
本文介绍了为什么 Thread.CurrentContext 属性和 Thread.GetDomain() 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

It's not a question of major importance, but I was wondering why the Thread class exposes a property for getting the current Context (Thread.CurrentContext) and a method for getting the current AppDomain (Thread.GetDomain()).

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

Knowing the hierarchy of Process > AppDomain > Context > Thread, my assumption would be that the context for thread is known at current point in time, and the domain needs to be searched based on current context.

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

But I'd like to hear wiser answers. Thanks!

推荐答案

我的假设是线程的上下文当前是已知的时间点,需要根据当前搜索域上下文.

my assumption would be that the context for thread is known at current point in time, and the domain needs to be searched based on current context.

确实,在 .NET Framework 的当前实现中,Context 对象 保留对其父域的引用.框架设计者可能已将上下文域公开为 Thread.Context.Domain.他们为什么不这样做可能是一个修辞问题;我无法通过查看参考源代码来判断.

Indeed, in the current implementation of the .NET Framework the Context object keeps a reference to its parent domain. The Framework designers might have exposed the context's domain as Thread.Context.Domain. It probably would be a rhetorical question why they didn't do so; I can't tell that by looking into the reference source code.

重要的是,在任何给定时刻,线程都在特定域内执行代码.这将是进程的默认域,或通过 AppDomain.DoCallBackAppDomain.ExecuteAssembly 或编组的 MarshalByRefObject 对象输入的域.那将是域 Thread.GetDomain() 返回.

What matters is that at any given moment of time, the thread is executing code inside a particular domain. This would be either the process's default domain, or the domain entered via AppDomain.DoCallBack, AppDomain.ExecuteAssembly or a marshalled MarshalByRefObject-object. That'd would be the domain Thread.GetDomain() returns.

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

This domain has at least one context (the default one), but it may also have other contexts, created for ContextBoundObject-objects. It's possible to enter any of those contexts explicitly on the same domain via Context.DoCallBack or implicitly from any domain by calling a marshalled ContextBoundObject-object. That'd be the context Thread.Context returns.

线程与域或线程与上下文之间没有父子关系.但是,域与其上下文之间存在严格的父子关系,一对多关系.因此,不需要根据当前上下文搜索域.

There is no parent-child relationship between thread and domain or thread and context. However, there is strict parent-child, one-to-many relationship between domain and its contexts. So, the domain doesn't need to be searched based on current context.

如果你更喜欢玩它,这是我用过的应用程序:

If you like to play with it a bit more, here is the app I used:

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.CurrentContext 属性和 Thread.GetDomain() 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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