Thursday, July 3, 2008

Multi Thread Programming in .NET

What is multi thread programming?

Multi thread programming allows you to do two or more tasks at the same time. As a windows user you have experienced doing two or more tasks together for example while you are listening to music you are using word at the same time you may printing a picture as well. It means that your computer has the ability of doing some tasks simultaneously. Multi thread programming gives you a chance to do this with different parts of the code.

What is process?

Process is a space assigned by CPU allows the code to be run in that space.

What is Thread?

Each process has at least one Thread which is a reference for CPU to run next line in that process

A better definition for Multi threads programming:

Create different thread in current process to run two different parts of the code simultaneously.

Why do we need Multi thread programming?

Each process has at least one thread that run the code called main thread. Sometimes we cannot hang main thread because it is reflecting the hang issue to user. Let me give you an example. Let's say we have a windows application and in part of our code we need to run an external application which may take 1 minute to run. But we cannot spend 1 minute to run this application because the main thread is responsible to render user interface as well so we may create a new thread to do this task for us and leave the main thread to take care of user interface. You see sometimes without multi thread programming our code is not efficient.

How multi thread programming works in .net?

Thanks to .net framework where you can find so many ways to implement multi thread programming. Even you can find this awesome technology in ado.net and UI and... I am going to start from very basic one which is the simple Thread class.

First rule in multi thread programming is that you can run part of your code in another thread as long as your code is defined in a method. It means that if you need to run part of your code in new thread you need to define that code in a method. So a new thread can be run over a method. In .Net there are two classes that help you to do this first is Thread class and second ThreadStart delegate. By ThreadStart you define the address of the method you need to run in a new Thread. By Thread class you start the Thread. Take a look at this code:

ThreadStart ts = new ThreadStart(myfunction);

Thread t = new Thread(ts);

t.Start();


In above code first myfunction address is assigned to ts delegate then a new object of Thread is created and in constructor the ts is passed to thread object so when Start is called it will start the new thread for ts which is actually myfunction.

I was always asked by students where do we use this how we can use this in a web based application it may not be useful for a web based application. But the reality is that you can use this even in a web based application in next post I show you how! I was assigned a small project that I had to use multi thread programming

3 comments:

Nahid said...

Hey Emad!
Happy to meet you again. I was really missed your posts.
Wish you LUCK :x

Emad Yazdanpanah said...

Hi Nahid,

Sorry I was extremly engaged with changing job so I did not have time to come to the site for a month.
Sorry!
Be ready for some nice tips about design patterns!

Chammi said...

Hi,
It is realy very nice. It is better to explain with all combination of thread props and methods in a sample.
Very good.

by
Shanmugam
Chennai