Every thread has an execution context data structure associated with it . The execution context includes things such as security settings (compressed stack, Thread’s Principal property, and Windows identity), host settings (see System.Threading.HostExecutionContextManager), and logical call context data (see System.Runtime.Remoting.Messaging.CallContext’s LogicalSetData and LogicalGetData methods) . When a thread executes code, some operations are affected by the values of the thread’s execution context settings . This is certainly true for the security settings . Ideally, whenever a thread uses another (helper) thread to perform tasks, the issuing thread’s execution context should flow (be copied) to the helper thread . This ensures that any operations performed by helper thread(s) are executing with the same security settings and host settings . It also ensures that any data stored in the initiating thread’s logical call context is available to the helper thread .
每一个线程都有和其关联的执行上下文,执行上下文其实是一个数据结够。执行上下文包含着诸如安全上下文:线程堆栈,线程标识;宿主上下文;调用上下文。这些上下文的代码的值会影响线程对代码的执行。对于安全上下文这中情况是肯定存在的。理想的情况下,当一个主线程使用其他辅助线程执行任务的时候,主线程的上下文会被传输到辅助线程的上下文。这样能确保辅助线程在相同上下文的环境下执行任何操作。这也使主线程的调用上下文能被辅助线程所使用。
By default, the CLR automatically causes the initiating thread’s execution context to flow to any helper threads . This transfers context information to the helper thread, but it comes at a performance cost because there is a lot of information in an execution context, and accumulating all of this information and then copying it for the helper thread takes a fair amount of time . If the helper thread then employs additional helper threads, then more execution context data structures have to be created and initialized as well .
在默认情况下,CLR会自动的将主线程的执行上下文传输给辅助线程。但是这会造成性能上的消耗,因为执行上下文包含着很多信息。如果辅助线程再调用一个辅助线程,这就意味着会有更多的信息再次被传输并且初始化上下文的数据结构。
In the System.Threading namespace, there is an ExecutionContext class that allows you control how a thread’s execution context flows from one thread to another . Here is what the class looks like:
在System.Threading命名空间下,通过ExecutionContext类,你可以控制线程执行上下文在线程间调用时的复制。下面是这个类的代码:
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
2 [SecurityCritical] public static AsyncFlowControl SuppressFlow();
3 public static void RestoreFlow();
4 public static Boolean IsFlowSuppressed();
5
6 // Less commonly used methods are not shown
7 }
You can use this class to suppress the flowing of an execution context, thereby improving your application’s performance . The performance gains can be quite substantial for a server application . There is not much performance benefit for a client application, and the SuppressFlow method is marked with the [SecurityCritical] attribute, making it impossible to call in some client applications (like Silverlight) . Of course, you should suppress the
flowing of execution context only if the helper thread does not need or access the context information . If the initiating thread’s execution context does not flow to a helper thread, the helper thread will use whatever execution context it last associated with it . Therefore, the helper thread really shouldn’t execute any code that relies on the execution context state (such as a user’s Windows identity) .
你可以通过这个类来控制执行上下文的传输(注:怎么控制?)从而提高应用程序的性能。在服务端性能能获得实质性的提升,但是在客户端则不一定(注:为什么?),而且由于SuppressFlow方法增加的SecurityCritical特性,使我们不能在客户端调用到它。你可以阻止上下文的传输当辅助线程不需要这些上下文信息的时候。当调用辅助线程的线程没有传输上下文给辅助线程的时候,辅助线程将使用离他层次最近的线程的上下文,因为这可能会造成辅助线程意外执行其他线程上下文的代码。