This site was last updated:Monday 13 October 2008, 11:30 GMT

Educational Community

THREADING IN C#
(2 votes, average: 3.00 out of 5)
Written by Christopher   
Sunday, 15 June 2008 15:14

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 (0)add comment

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

busy
 

User Menu

None
Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor KNOGE.com offers free video and text tutorials on various softwares also free resources to improve your economy and start making money online. THIS IS ONLY A TEST AD TO DISPLAY HOW IT MIGHT LOOK Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor

Adobe InDesign CS3 - Working with objects

Objects in Adobe InDesign CS3 are the basic building block of any design that you do. In this video tutorial you will learn how to create those blocks easily in...

InDesign Videos | Christopher

Read More

Adobe InDesign CS3 - Working with Panels & More

In this Adobe InDesign CS3 video tutorial you will learn how to set keys and work with panels, you will also learn how to create customized keyboard shortcuts for optimized...

InDesign Videos | Christopher

Read More

Adobe Photoshop CS3 - Create customized rust on cars

This Adobe Photoshop CS3 video tutorial will teach you how to create your own customized rust on cars for a more grungy and perhaps dusty effect. This technique does also...

Photoshop Videos | Christopher

Read More
100%
-
+
3
Show options