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

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

问题描述

这不是主要的问题,但是我想知道为什么Thread类公开了用于获取当前Context的属性( 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天全站免登陆