Total Pageviews

Thursday, August 2, 2012

Threads overview

Threads are the operating system construct used by the common language runtime to execute code. At run time, all managed code is loaded into an application domain and is run by a managed thread.
There is not a one-to-one correlation between application domains and threads. Several threads can be executing in a single application domain at any given time and a particular thread is not confined to a single application domain. That is, threads are free to cross application domain boundaries; a new thread is not created for each application domain.

At any given time, every thread is executing in an application domain. Zero, one, or more than one thread might be executing in any given application domain. The run time keeps track of which threads are running in which application domains. You can locate the domain in which a thread is executing at any time by calling the GetDomain method.
You can attach a CultureInfo object to a thread. However, to prevent malicious code from entering other application domains, the CultureInfo object is automatically set to read-only when its thread crosses an application domain boundary.

using System;
  using System.Threading;
 
  class Test
  {
      static void Main()
      {
          Thread newThread = new Thread(new ThreadStart(ThreadMethod));
          newThread.Start();
      }
 
      static void ThreadMethod()
      {
          Console.WriteLine(
              "Thread {0} started in {1} with AppDomainID = {2}.",
              AppDomain.GetCurrentThreadId().ToString(),
              Thread.GetDomain().FriendlyName,
              Thread.GetDomainID().ToString());
      }
  }

No comments:

Post a Comment