Resources » .NET programming » .NET Framework
Introducing Multithreaded Programming in C#.
This article explains in detail about multithreading,how to create a thread and lots more..
Although C# contains many innovative features, one of the most exciting is its built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution. Thus, multithreading is a specialized form of multitasking.
There are two distinct types of multitasking: process-based and thread-based.
The differences between process-based and thread-based multitasking can be summarized like this:
Process-based multitasking handles the concurrent execution of programs, While Thread-based multitasking deals with the concurrent execution of pieces of the same program.
Process-based: Example – running word processor the same time you are browsing the net.
Thread-based: Example – A text editor can be formatting text at the same time that it is printing.
The principal advantage of multithreading is that it enables you to write very efficient programs because it lets you utilize the idle time that is present in most programs.
A thread can be in one of several states. It can be running. It can be ready to runas soon as it gets CPU time. A running thread can be suspended, which is a temporary halt to its execution. It can later be resumed, A thread can be blocked when waiting for a resource. A thread can be terminated, in which case its execution ends and cannot be resumed.
The .Net Framework defines two types of threads: foreground and background.
By default when you create a thread, it is a foreground thread, but you can change it to a background thread. The only difference between a foreground and background thread is that a background thread will be automatically terminated when all foreground threads in its process have stopped.
All processes have at least one thread of execution, which is usually called the main thread because it is the one that is executed when your program begins. From the main thread you can create other threads.
The classes that support multithreaded programming are defined in the System.Threading namespace. Thus, you will usually include this statement at the start of any multithreaded program:
Creating a Thread
To create a thread, you instantiate an object of type Thread. Thread defines the following constructor:
public Thread( ThreadStart entrypoint)
Here, entrypoint is the name of the method that will be called to begin execution of the thread. ThreadStart is a delegate defined by the .NET Framework as shown here:
public delegate void ThreadStart()
Thus, your entrypoint method must have a void return type and take no arguments.
Once created, the new thread will not start running until you call its Start() method, which is defined by Thread. The Start() method is shown here:
public void Start()
Once started, the thread will run until the method specified by entryPoint returns. Thus, when entryPoint returns, the thread automatically stops. If you try to call Start() on a thread that has already been started, a ThreadStateException will be thrown.
//Create a thread of execution.
public int count;
public MyThread(string name)
count = 0;
threadName = name;
//Entry point of thread.
public void run()
Console.WriteLine(threadName + " starting.");
Console.WriteLine("In " +threadName + ",count is " +count);
}while(count < 10);
Console.WriteLine(threadName + " treminating.");
public static void Main()
Console.WriteLine("Main thread starting.");
// First, construct a MyThread object.
MyThread myThread = new MyThread("child #1");
//Next, construct a thread from that object.
Thread newThread = new Thread(new ThreadStart(myThread.run));
//Finally, start execution of the thread.
}while (myThread.count != 10);
Console.WriteLine("Main thread ending.");
Notice the call to Sleep(), which is a static method defined by Thread.
The Sleep() method causes the thread from which it is called to suspend execution for the specified period of milliseconds. The form used by the program is shown here:
public static void Sleep(int milliseconds)
The number of milliseconds to suspend is specified in milliseconds. If milliseconds is zero, the calling thread is suspended only to allow a waiting thread to execute.
Here’s the output:-
Main thread starting.
.child #1 starting.
.....In child #1,count is 0
.....In child #1,count is 1
.....In child #1,count is 2
.....In child #1,count is 3
.....In child #1,count is 4
.....In child #1,count is 5
.....In child #1,count is 6
.....In child #1,count is 7
.....In child #1,count is 8
.....In child #1,count is 9
child #1 treminating.
Main thread ending.
While the preceding program is perfectly valid, some easy improvements will make it more efficient. First, it is possible to have a thread begin execution as soon as it is created. In the case of MyThread this is done by instantiating a Thread object inside MyThread's constructor. Second, there is no need for MyThread to store the name of the thread since Thread defines a property called Name.
public int count;
public Thread thread;
public MyThread(string name)
count = 0;
thread = new Thread(new ThreadStart(this.run));
thread.Name = name; // set the name of the thread
thread.Start(); // start the thread
View My Next Article: "Determining when a Thread Ends"
Did you like this resource? Share it with your friends and show your love!
|Author: Chandramohan 19 Jun 2004||Member Level: Bronze Points : 0|
|If I use public Thread(ThreadStart entrypoint), the compiler thrown the error. the err msg is |
must declare a body because it is not marked abstract or extern
|Author: Janak Ram 03 Jul 2004||Member Level: Bronze Points : 0|
|You did a very good job explaining the threading in .NET. A good nice example would be of help. Lot of help. A working solution is recommended with all articles.|
Very informative article.
|Author: Carlos Abu Jason 24 Oct 2004||Member Level: Bronze Points : 0|
|Thank you Anita, well done. Now I understand C# thread handling. This rules!|
|Author: Neil Riddle 12 Nov 2004||Member Level: Bronze Points : 0|
Thanks for your ariticles on Threading. I'm inexperienced with C#. Your articles have been very beneficial.
|Author: Samuel 18 May 2005||Member Level: Bronze Points : 0|
|I was browsing through the book " C# The Complete Reference" by Herbert Schildt, and realized that the article contents and the examples were taken from this book. So in case anyone feels like geting more details about concept of multithreading refer to the above book, Chapter 21. |
|Author: Pallav 22 Aug 2005||Member Level: Silver Points : 0|
i read you article. You had explained the theory of threading very well. I appriciate you for that
But you hadn't explained the examples in detail.
Please edit to give appropriate examples with explanation.
Return to Article Index
Active MembersTodayLast 7 Daysmore...
Talk to Webmaster Tony John