Saturday, December 20, 2008

Another resource for Multithreading in C#

Introduction:

In this article let us see about multithreading. Multithreaded applications provide the illusion that numerous activities are happening at more or less the same time. In C# the System.Threading namespace provides a number of types that enable multithreaded programming.

Threading in C#

System.Threading Namespace

The System.Threading Namespace provides a number of types that enable multi-threading programming. In addition to providing types that represent a particular thread, this namespace also describe types that can handle a collection of threads (Thread Pool), a simple (Non -GUI based) Timer class and several types to provide synchronized access to shared data.

The most primitive of all types in the System.Threading namespace is Thread. Thread:

Represents a thread that executes with in the CLR. Using this type, one can able to spawn additional threads in the owning AppDomain.This type defines a number of methods (both static and shared) that allow us to create new threads from a current thread, as well as suspend, stop, and destroy a given thread.

The following example will demonstrate Spawning of Secondary threads using C#.

internal class EmployeeClass
{
public void Performjob()
{

// Get some information about the thread.

Console.Writeline("ID of Employee thread is {0}",
Thread.CurrentThread.GetHashCode() );

//Do the job.
Console.write("Employee says:");
for (int i=0;i<10;i++)
{
Console.write(i +",");
}
console.writeline();
}
}
The above code simply prints out a series of numbers by way of the performjob() member function.

Now assume that a class (Main class) creates a new instance of EmployeeClass. In order for the Main class to continue processing its workflow, it creates and starts a new thread that is used by the job. In the code below, notice that Thread type requests a new Threadstart delegate type:

Public class Mainclass
{
public static int Main (string[] args)
{
Console.Writeline("ID of primary thread is {0}",
Thread.CurrentTHread.GetHashCode() );

// Make Employee class.

EmployeeClass j = new EmployeeClass();

// Now make and start the background thread.

Thread backgroundThread =
new Thread(new ThreadStart(j.Performjob));
backgroundThread.Start();
return 0;
}
}
If we run the application we would find the following result.
ID of primary thread is: 2
ID of Employee thread is: 11
Employee says: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
Press any key to continue
From the above example one can easily know how to spawning the secondary Threads.

Let us see another example regarding two active threads.

internal class EmployeeClass
{
public void Performjob()
{
// Get some information about the thread.

Console.Writeline("ID of Employee thread is {0}",
Thread.CurrentThread.GetHashCode() );

//Do the job.
Console.write("Employee says:");
for (int i = 0; i < 20000 ; i++)
{
Console.write(i +",");
}
console.writeline();
}
}

Public class Mainclass
{
public static int Main (string[] args)
{
// Make Employee class
............
//Now make the thread.
............
//Now while background thread is working
//do some additional work.
MessageBox.show("BUSY");
return(0);
}
}
If we run this application we would see that the message box is displayed and it can be moved around the desktop, while the background Employee thread is busy pumping numbers to the console.

Thread Synchronization:

A multithreaded application usually has resources that can be accessed from multiple threads; for example, a global variable that is incremented or decremented by multiple threads. It is sometimes desirable to prevent multiple threads from concurrently altering the state of a resource. The .NET Framework includes several classes and data types that we can use to synchronize actions performed by two threads.

The simplest case is if we have a shared variable that we need to update from different threads. To do this, we can use the System.Threading.Interlocked class. For example, to increment or decrement the shared variable called num, we'd write Interlocked.Increment(num) or Interlocked.Decrement(num). we can also use Interlocked to set the variables to a specific value or to check the equality of two variables.

If there's a section of code in an object's method that should not be accessed concurrently by multiple threads, we can use the Monitor class to acquire a lock on that object by calling Monitor.Enter(object). Any other thread wanting to execute the same code would need to acquire the same lock and will be paused until the first thread releases the lock by calling Monitor. Exit(object).

For more control over thread synchronization or for cross-process synchronization, use the Mutex class, which is a named synchronization object that can be obtained from any thread in any process. Once we create or obtain the mutex, we use its GetHandle method to (as we'd expect) get a handle that we can use with the WaitHandle.WaitAny or WaitHandle.WaitAll methods. These two methods are blocking and will return only if the specified handle is signaled (that is, the mutex is not being used by another thread) or if the specified timeout expires. After we obtain the mutex, we perform the necessary synchronized processing and then call Mutex.ReleaseMutex to release it.

Sometimes we need a mechanism for one thread to notify other threads of some interesting event that occurred. In those cases we can use the .NET synchronization event classes, ManualResetEvent and AutoResetEvent. In the world of thread synchronization, an event is an object that has two states: signaled and nonsignaled. An event has a handle and can be used with WaitHandle just like a mutex. A thread that waits for an event will be blocked until another thread signals the event by calling ManualResetEvent.Set or AutoResetEvent.Set. If we are using a ManualResetEvent, we must call its Reset method to put it back to the nonsignaled state. An AutoResetEvent will automatically go back to nonsignaled as soon as a waiting thread is notified that the event became signaled.

Conclusion:

That's all it takes to create a multi-threaded application in C#.The System.Threading namespace in .NET SDK makes multi-threading easy.The System.Threading Namespace provides a number of types that enable multi-threading programming easily in C#. The Thread class in the System.Threading namespace exposes the properties and methods to allow the free threading.

0 comments: