Current area: HOME -> MULTITHREADING IN C#
|
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 you 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.
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.
About the Author:
|
G.GNANA ARUN GANESH is the Administrator and the Founder of
ARUN MICRO SYSTEMS. He has been programming in C++, Visual Basic, COM, Java
and Microsoft Technologies for 3+ years.
Arun's background includes Bachelor degree in
ECE.He is one of the Top authors writing articles in C# and .NET in various websites.
He is an Active person in the panel of Technical Reviewers in Prentice Hall Publishers
and Sams Publication. In .NET the last book he reviewed is the "C# how to program"
written by Harvey and Paul Deitel. You can contact him for technological support and
queries through ggarung@rediffmail.com.
|
| User Comments |
 |
 |
|
john bailo
     From kent, wa
(Report as abusive)
|
good article -- sample code a little off capitalization on the sample code is off -- so had to edit a little w/cut & paste... great content |
Anonymous
    
(Report as abusive)
|
Simple, Helpful I love samples like this, I don't need everything spelled out for me... just a lil demo. Perfect. |
Anonymous
    
(Report as abusive)
|
helped me a lot thanks! helped me figure out how to wait for thread pools to finish before the main thread. thanks again! |
John
     From NJ
(Report as abusive)
|
Simple and Useful Nice article. It is so simple to understand. Thanks. |
Anonymous
    
(Report as abusive)
|
Very bad This article hasn't provided any real in-depth information. Even a person who doesn't know programming multithreading application can borrow example from C# book and make the example. It doesn't touch hard-core. |
| |
View all
Rate and comment this article
|
Think you've can write or have written a good article?
Want to get it hosted for free on a highly respected programming site?
Get the attention you deserve! Click here to find our more
|