Pages

Dec 23, 2008

Threads Implementation in C# + source code

Thread in C# :
Introduction to Threads In C# :
Threads: Threads are often called lightweight processes. However they are not process.es

A Thread is a small set of executable instructions, which can be used to isolate a task from a process.
Multiple threads are efficient way to obtain parallelism of hardware and give interactive user interaction to your applications.

C# Thread:.
. Net Framework has thread-associated classes in System.Threading namespace. The following steps demonstrate how to create a thread in C#.

Step 1. Create a System.Threading.Thread object.
Creating an object to System.Threading.Thread creates a managed thread in .Net environment. The Thread class has only one constructor, which takes a ThreadStart delegate as parameter. The ThreadStart delegate is wrap around the callback method, which will be called when we start the thread.


Step 2: Create the call back function
This method will be a starting point for our new thread. It may be an instance function of a class or a static function. Incase of instance function, we should create an object of the class, before we create the ThreadStart delegate. For static functions we can directly use the function name to instantiate the delegate. The callback function should have void as both return type and parameter. Because the ThreadStart delegate function is declared like this. (For more information on delegate see MSDN for “Delegates”).


Step 3: Starting the Thread.
We can start the newly created thread using the Thread’s Start method. This is an asynchronous method, which requests the operating system to start the current thread.
For Example:
// This is the Call back function for thread.

Public static void MyCallbackFunction()
{
while (true)
{
System.Console.WriteLine(“ Hey!, My Thread Function Running”); ………
}
}

public static void Main(String []args)
{
// Create an object for Thread
Thread MyThread = new Thread(new ThreadStart (MyCallbackFunction));
MyThread.Start() ……
}

Killing a Thread:
We can kill a thread by calling the Abort method of the thread. Calling the Abort method causes the current thread to exit by throwing the ThreadAbortException.

MyThread.Abort();

Suspend and Resuming Thread:

We can suspend the execution of a thread and once again start its execution from another thread using the Thread object’s Suspend and Resume methods.
MyThread.Suspend();

// causes suspend the Thread Execution.
MyThread.Resume() ;
// causes the suspended Thread to resume its execution.


Thread State:
A Thread can be in one the following state.
Unstarted - Thread is Created within the common language run time but not Started still.
Running - After a Thread calls Start method
WaitSleepJoin - After a Thread calls its wait or Sleep or Join method.
Suspended - Thread Responds to a Suspend method call.
Stopped - The Thread is Stopped, either normally or Aborted.

We can check the current state of a thread using the Thread’s ThreadState property.


Thread Priorty:
The Thread class’s ThreadPriority property is used to set the priority of the Thread.

A Thread may have one of the following values as its Priority:

Lowest

BelowNormal

Normal

AboveNormal

Highest.

The default property of a thread is Normal.

REF: Code project

2 comments:

Anonymous said...

Great post.
I began researching about threads reading your post in Christmas.
Then, I began researching about multicore programming with C#.
I bought a new book for beginners by Packt Publishing: "C# 2008 and 2005 Threaded Programming: Beginner’s Guide", by Gaston C. Hillar - http://www.packtpub.com/beginners-guide-for-C-sharp-2008-and-2005-threaded-programming/book
It is a book writen for C# programmers (beginners) who want to exploit multi-core with C# 2005; 2008 and future 2010.
I am reading the book and it has great exercises to help developers run in the multi-core jungle. Highly recommended.

Anonymous said...

Thank you Ariel. The books you recommended are really good.

Thanks
Mukunda