Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExecutionContext of Threads

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/>";
         }
      }
   }
}
like image 348
S M Kamran Avatar asked Dec 23 '09 10:12

S M Kamran


People also ask

How Scala is executed?

Scala uses Java Virtual Machine (JVM) to execute bytecode. Its code is compiled into bytecode and executed by Java Virtual Machine. So you need only JVM to start development with it. Scala can also use all java classes and allows us to create our custom class.

What is execution context in Java?

An "execution context class" is just a holder class, created by the top level of your program, which holds all of the things (like the Transaction object in the linked example) from the top level which might be needed. It's a packaged way to avoid global variables.

What is execution context in Scala?

An ExecutionContext can execute program logic asynchronously, typically but not necessarily on a thread pool. A general purpose ExecutionContext must be asynchronous in executing any Runnable that is passed into its execute -method.

Which of the following structures of system Threading namespace provides the functionality to restore the migration or flow of the execution context between threads?

Structs. Provides the functionality to restore the migration, or flow, of the execution context between threads.


1 Answers

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.

like image 194
Hans Passant Avatar answered Oct 17 '22 15:10

Hans Passant