线程的执行上下文

发布于 2024-08-16 02:25:42 字数 1407 浏览 5 评论 0原文

ExecutionContext.SuppressFlow(); 的用途是什么?在下面的代码中到底什么被抑制了?

我有这个测试代码......

protected void btnSubmit_Click(object sender, EventArgs e)
{
   Thread[] th = new Thread[100];
   Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-GB");

   AsyncFlowControl cntrl = ExecutionContext.SuppressFlow();
   for (int i = 0; i < th.Length; i++)
   {                   
      th[i] = new Thread(new ParameterizedThreadStart(ThreadMethod));
      th[i].Name = "Thread #" + (i+1).ToString();                
      th[i].Start((i+1).ToString());
   }
   ExecutionContext.RestoreFlow();

   foreach (Thread t in th)            
   {
      t.Join();
   }
   Response.Write(response);
}


String response = null;
Random rnd = new Random(1000);
private void ThreadMethod(object param)
{   
   if (param != null)
   {
      string temp = param as string;
      if (temp != null)
      {
         //To test what is the current culture I get for this thread execution
         System.Globalization.CultureInfo info = Thread.CurrentThread.CurrentCulture;
         for (int i = 0; i <= 10; i++)
         {
            Thread.Sleep(rnd.Next(2000));
            response += Thread.CurrentThread.ManagedThreadId.ToString() + ":" 
                     + Thread.CurrentThread.Name + ": " + temp + "<br/>";
         }
      }
   }
}

What's the purpose of ExecutionContext.SuppressFlow();? In the following code what exactly gets suppressed?

I've this test code...

protected void btnSubmit_Click(object sender, EventArgs e)
{
   Thread[] th = new Thread[100];
   Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-GB");

   AsyncFlowControl cntrl = ExecutionContext.SuppressFlow();
   for (int i = 0; i < th.Length; i++)
   {                   
      th[i] = new Thread(new ParameterizedThreadStart(ThreadMethod));
      th[i].Name = "Thread #" + (i+1).ToString();                
      th[i].Start((i+1).ToString());
   }
   ExecutionContext.RestoreFlow();

   foreach (Thread t in th)            
   {
      t.Join();
   }
   Response.Write(response);
}


String response = null;
Random rnd = new Random(1000);
private void ThreadMethod(object param)
{   
   if (param != null)
   {
      string temp = param as string;
      if (temp != null)
      {
         //To test what is the current culture I get for this thread execution
         System.Globalization.CultureInfo info = Thread.CurrentThread.CurrentCulture;
         for (int i = 0; i <= 10; i++)
         {
            Thread.Sleep(rnd.Next(2000));
            response += Thread.CurrentThread.ManagedThreadId.ToString() + ":" 
                     + Thread.CurrentThread.Name + ": " + temp + "<br/>";
         }
      }
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遗忘曾经 2024-08-23 02:25:42

ExecutionContext 的细节非常模糊,深埋在 .NET Remoting 和 WCF 等功能的内部。
它的一部分是:

  • HostExecutionContext
  • IlologicalCallContext,Remoting LogicalContext 使用的线程特定数据的存储库
  • ,如上所述
  • SecurityContext
  • SynchronizationContext

CultureInfo 不是其中的一部分,如果更改主线程的默认区域性,这可能是一个相当大的问题。除非您显式编写代码来切换其他线程,否则没有什么好方法可以确保其他线程以该区域性运行。鉴于 .NET 很容易在线程池线程上运行异步回调,这并不总是可行。它们将被初始化为系统默认区域性。

编辑:此问题在 .NET 4.5 中通过 CultureInfo.DefaultThreadCurrentCulture 属性得到修复。

Edit2:.NET 4.6 中的修复更加彻底,文化现在按预期流动。

The details of ExecutionContext are very obscure, buried deep inside features like .NET Remoting and WCF.
What is part of it is:

  • HostExecutionContext
  • IllogicalCallContext, a repository of thread specific data used by Remoting
  • LogicalContext, as above
  • SecurityContext
  • SynchronizationContext

CultureInfo is not part of it, which can be a considerable problem if you change your main thread's default culture. There is no good way to ensure other threads run with that culture unless you explicitly write the code to switch them. That's not always practical, given that .NET is apt to run async callbacks on threadpool threads. They will be initialized to the system default culture.

Edit: this problem got fixed in .NET 4.5 with the CultureInfo.DefaultThreadCurrentCulture property.

Edit2: fixed much more thoroughly in .NET 4.6, culture now flows as expected.

顾铮苏瑾 2024-08-23 02:25:42

ExcecutionContext.SuppressFlow 抑制跨异步线程的执行上下文流。

ExecutionContext从父线程隐式传递到子线程,提供与逻辑执行线程相关的信息:安全上下文、调用上下文和同步上下文。如果该信息不是必需的,则省略执行上下文会稍微优化多线程应用程序的性能。

ExecutionContext.RestoreFlow恢复线程之间执行上下文的通道。

最后

Q在下面的代码中到底什么被抑制了?

A:到底被抑制了以下信息的传递:安全上下文、调用上下文和同步上下文;在新创建的线程之间。 为什么这样做? -优化th.Length创建的线程的创建和工作:线程之间传递的补充信息更少 - 线程之间的交互速度更快。

ExcecutionContext.SuppressFlow suppresses the flow of the execution context across asynchronous threads.

The ExecutionContext, are implicitly passed from parent thread to the child one, provides information relevant to a logical thread of execution: security context, call context and synchronization context. If that information is not imperative, the omission of the execution context optimize a little the performance of a multithreading application.

ExecutionContext.RestoreFlow restores the passage of the execution context between threads.

Finally

Q: In the following code what exactly gets suppressed??

A: Exactly are suppressed the passage of the following information: security context, call context and synchronization context; between the newly created threads. Why that was do? -To optimize the creation and work of th.Length created threads: less supplementary information passed between threads - quicker this threads interact between them.

初熏 2024-08-23 02:25:42

不是您问题的答案,但由于您现在正在查看此代码并尝试理解它,请检查您是否要根据文档调整/更改代码(即“修复它”):

ExecutionContext.SuppressFlow:

您必须对返回的 AsyncFlowControl 结构使用 Undo 方法来恢复 ExecutionContext 的流程。

ExecutionContext.RestoreFlow:

RestoreFlow 反转先前 SuppressFlow 方法调用的效果。

此方法由 SuppressFlow 方法返回的 AsyncFlowControl 结构的 Undo 方法调用。 您应该使用 Undo 方法来恢复执行上下文的流程,而不是 RestoreFlow 方法。

强调我的。

Not the answer to your question, but since you're looking at this code and try to understand it right now, please check if you want to adapt/change your code according to the documentation (i.e. "fix it"):

ExecutionContext.SuppressFlow:

You must use the Undo method on the returned AsyncFlowControl structure to restore the flow of the ExecutionContext.

ExecutionContext.RestoreFlow:

RestoreFlow reverses the effect of a prior SuppressFlow method call.

This method is called by the Undo method of the AsyncFlowControl structure returned by the SuppressFlow method. You should use the Undo method to restore the flow of the execution context, not the RestoreFlow method.

Emphasis mine.

時窥 2024-08-23 02:25:42

我读到了这样的内容:“创建线程时,运行时确保启动线程的执行上下文流向新线程。这样,新线程具有与父线程相同的特权。
然而,这种数据复制确实会消耗一些资源。如果您不需要此数据,您可以使用 ExecutionContext.SuppressFlow 方法禁用此行为。”

来源:C# 考试编程 70-483。作者:Wouter de Kort

您可以通过以下方式调用 ExecutionContext.SuppressFlow() 来提高性能:减少应用程序的执行时间,因为方法调用将阻止将上下文数据从父线程复制到子线程,否则当您可能不需要子线程中的父线程的上下文数据时,这会影响或增加执行时间。

I read this - "When a thread is created, the runtime ensures that the initiating thread’s execution context is flowed to the new thread. This way the new thread has the same privileges as the parent thread.
This copying of data does cost some resources, however. If you don’t need this data, you can disable this behavior by using the ExecutionContext.SuppressFlow method."

Source : Programming in C# Exam 70-483. Author : Wouter de Kort

You would call ExecutionContext.SuppressFlow() to improve performance by reducing execution time of the application as the method call will prevent copying of context data from parent thread to the child thread that would otherwise impact or increase the execution time when you may not require the context data of the parent thread in the child thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文