Tutorials
Desktop Programming
C Sharp
THREADING IN C#
Written by Christopher Sunday, 15 June 2008 15:14
Tutorials
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.
Max
said:
|
... 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 |
|
Votes: +0
| < Prev | Next > |
|---|
| Training Tip #1 :: Push-ups on the Swiss Ball admin 28.4.2009 7:07 |
Re:hello admin 21.4.2008 14:11 |
| Dell Inspiron 1545 Windows XP Drivers admin 12.4.2009 8:03 |
hello yuppy 21.4.2008 10:28 |
| Re:Get rich ebooks - are they really working? lann 23.4.2008 17:04 |
Re:Get rich ebooks - are they really working? admin 19.4.2008 14:08 |