Text Size

THREADING IN C#

PDF 

Tutorials

(3 votes, average: 3.67 out of 5)

INTRODUCTION

Multi-core processors are new standards on the user’s hardware. By taking advantage of the multi-processing capabilities of these machines you can achieve significant performance upgrade with some minor changes in your code. When an application does not make use of threading techniques it is wasting valuable resources. This tutorial will cover the basic threading concepts and threading methods and properties.


SIMPLE THREADS

The THREAD class is the basis for all multi-threading capabilities of your program. This class allows you to define and start, suspend or resume a thread operation, abort an existing thread and provides information about the status of the particular thread. In order to start a new thread you have to define a delegate (variable function) and assign it to a thread object. The following snippet of code illustrates a simple thread operation. Make sure you have added the “using System.Threading” statement on your project:

class Program

{

static void Main(string[] args)

{

ThreadStart threadStart1 = new ThreadStart(ThreadFunction);

Thread myThread = new Thread(threadStart1);

myThread.Start();

Console.ReadLine();

}

static void ThreadFunction()

{

Console.WriteLine("Hello World!!");

}

}

MULTIPLE THREADS

In the previous example we have defined a method that returns void and takes no parameters. We have also declared a delegate to that method. Next, we define a new thread and we execute it by calling the Start method of the Thread object. This is a fine example, but it doesn’t actually uses more than one thread. The next example defines four threads and executes them all:

class Program

{

static void Main(string[] args)

{

ThreadStart threadStart1 = new ThreadStart(ThreadFunction);

for (int i = 1; i <= 4; i++)

{

Thread myThread = new Thread(threadStart1);

myThread.Start();

}

Console.ReadLine();

}

static void ThreadFunction()

{

for (int i = 1; i < 10; i++)

{

Console.WriteLine(Thread.CurrentThread.ManagedThreadId);

Thread.Sleep(1000);

}

}

}

This program uses the same principles with the previous one with the difference that it defines and executes four threads. The method Thread.CurrentThread.ManagedThreadID() returns the ID of the thread, a number. Also, Thread.Sleep() defines, in milliseconds, for how much time the thread will go to sleep ( suspend). The program works as follows:

Four threads are defined in this program. The first thread is executed and the first ThreadID is printed on the screen. Then, the thread goes to sleep. This allows for the second thread to start executing and print its own Id on the screen. Then, it goes to sleep as well for one second. The same goes on for the other threads and so on. See the output and try to match the results with the procedure described here. Notice, that if you run the program again you will probably see different ID numbers. This is logical, since the processor has various threads it is working on at any given time.

Threads allow us to efficiently utilize cpu resources. In the previous multi-thread example, if we hadn’t defined different threads we would have to wait ten seconds to complete the first execution of the delegate method, ten for the second and so on. Multiple threads allowed us to make use of the time the cpu was supposed to sleep. Finally, priority levels can be defined for threads such as Highest, AboveNormal, and Lowest priority. However, your system might starve of resources should you assign the highest priority in many threads.
Trackback(0)
Comments (1)add comment

Max said:

0
...
How is this related to multi-core programming? This seems a misnomer the way you've presented it. This can even be done on single core chip and does not take advantage of multiple core of the CPU
 
January 08, 2009
Votes: +0

Write comment

busy

Site Statistics

Stats
Total Members
: 208
Total Discussion
: 0
Total Albums
: 0
Total Photos
: 0
Total Bulletins
: 0
Total Activities
: 1
Total Wall Posts
: 1