第27章中有一条声明,CLR通过里希特写的c#书:
您可以使用ExecutionContext类来抑制执行上下文的流动,从而提高应用程序性能。对于服务器应用程序,性能提升可能非常大。
其次是示例:
CallContext.LogicalSetData("Name", "Jeffrey");
//Initiate some work to be done by thread pool
ThreadPool.QueueUserWorkItem(
state => Console.WriteLine("Name={0}", CallContext.LogicalGetData("Name")
));
接下来是第二部分:
//Now suppress the flowing of the Main thread's execution context
ExecutionContext.SuppressFlow();
//Initiate some work to be done by thread pool
ThreadPool.QueueUserWorkItem(
state => Console.WriteLine("Name={0}", CallContext.LogicalGetData("Name")
));
ExecutionContext.RestoreFlow();
结果:
Name=Jeffrey
Name=
子线程已丢失其上下文(在示例的第2部分中),因为副作用参数在压制后丢失。但是我们节省了CPU时间(在我的情况下节省了大约10%的CPU时间)。