Threading

  • 3

Threading

Visit: http://www.zonixsoft.com

C# supports parallel execution of code through multithreading. A thread is an independent execution path, able to run simultaneously with other threads.A C# program starts in a single thread created automatically by the CLR and operating system (the “main” thread), and is made multi-threaded by creating additional threads.The CLR assigns each thread its own memory stack so that local variables are kept separate.

Threading enables your C# program to perform concurrent processing so you can do more than one operation at a time. The System.Threading namespace provides classes and interfaces that support multithreaded programming and enable you to easily perform tasks such as creating and starting new threads, synchronizing multiple threads, suspending threads, and aborting threads. The advantage of threading is the ability to create applications that use more than one thread of execution.

How Threading Works

Multithreading is managed internally by a thread scheduler, a function the CLR typically delegates to the operating system. A thread scheduler ensures all active threads are allocated appropriate execution time, and that threads that are waiting or blocked – for instance – on an exclusive lock, or on user input – do not consume CPU time.

On a single-processor computer, a thread scheduler performs time-slicing – rapidly switching execution between each of the active threads.

On a multi-processor computer, multithreading is implemented with a mixture of time-slicing and genuine concurrency – where different threads run code simultaneously on different CPUs. It’s almost certain there will still be some time-slicing, because of the operating system’s need to service its own threads – as well as those of other applications.

Killing a Thread:

We can kill a thread by calling the

 

Abort method of the thread.

MyThread.Abort();

Suspend and Resuming Thread:

We can suspend the execution of a thread and once again start its execution from another thread using the Thread object’s Suspend and Resume methods.

  MyThread.Suspend() // causes suspend the Thread Execution.

 

  MyThread.Resume() // causes the suspended Thread to resume its execution.

 

 

Creating and Starting Threads

Threads are created using the Thread class’s constructor, passing in a ThreadStart delegate – indicating the method where execution should begin.  Here’s how the ThreadStart delegate is defined:

public delegate void ThreadStart();

Calling Start on the thread then sets it running. The thread continues until its method returns, at which point the thread ends. Here’s an example, using the expanded C# syntax for creating a TheadStart delegate:

class ThreadTest {

  static void Main() {

    Thread t = new Thread (new ThreadStart (Go));

    t.Start();   // Run Go() on the new thread.

    ……

  }

  static void Go() { …..}

A thread can be created more conveniently using C#’s shortcut syntax for instantiating delegates:

static void Main() {

  Thread t = new Thread (Go);    // No need to explicitly use ThreadStart

  t.Start();

 

}

static void Go() { … }

In this case, a ThreadStart delegate is inferred automatically by the compiler. Another shortcut is to use an anonymous method to start the thread:

static void Main() {

  Thread t = new Thread (delegate() { Console.WriteLine (“Hello!”); });

  t.Start();

}

A thread has an IsAlive property that returns true after its Start() method has been called, up until the thread ends.A thread, once ended, cannot be re-started.