this blog contains information for .net and sql stuffs. You can find various tips and tricks to overcome problem you may be facing in ...

Thursday, May 6, 2010

Threading in .net (Part -1)

Most of the time developer chooses to develop program in linear way. In this mechanism user would need to wait sometime in some situation like application is going to download page from server, application is going to print document, applications is going to access remote database. Those cases are time consuming, user needs to wait till main thread completes work, sometime user get frustrated by response time, and application does not response to user till task has been completed.

To overcome, .net has provided threading mechanism. Our main thread executes as and in background we can execute process. So user will not feel that application is not responding, in background thread method executes and once result will be available, it would be shown to user.

Threading means in a single process execute more than one task among different processors. Now days most of computers are more than one core, we can use another core when main core is busy to complete task. We can distribute work task among different processors.

Though multithreading seems to be very complex, .net has provided a simple way to implement in programming. At a time there can be more than 250 threads run in back ground. We can even change it to more if require.

To work with thread we need to add System.Threading namespace to our application. We can run background thread by using ThreadPool.QueueUserWorkItem and passing method name or address of in vb.net.

static void Main(string[] args)

{

int workerThreads;

int completionPortThreads;

ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);

ThreadPool.QueueUserWorkItem(ThreadProc, "Thread 1");

ThreadPool.QueueUserWorkItem(ThreadProc, "Thread 2");

ThreadPool.QueueUserWorkItem(ThreadProc, "Thread 3");

ThreadPool.QueueUserWorkItem(ThreadProc, "Thread 4");

ThreadProc("ForgroundThread");

Thread.Sleep(1000);

Console.WriteLine("Worker Threads: {0} CompletePortThreads: {1}", workerThreads, completionPortThreads);

ThreadPool.QueueUserWorkItem(ThreadProc, "Thread 5");

ThreadPool.QueueUserWorkItem(ThreadProc, "Thread 6");

Console.WriteLine("Now main is continue");

}

Above code shows how many threads can be run, default value is 250. We have used ThreadProc method which will run in background thread. When main thread has no work at that time background thread will run and execute code. We can add as many threads as we wish as per limit of 250 and if required we can increase this maximum thread limit.

Thread which runs first is called Forground and other called background.

static void ThreadProc(object msg)

{

string threadMsg = (string)msg;

if (Thread.CurrentThread.IsBackground)

{

Console.WriteLine("Background Thread");

Console.WriteLine("My Threading method with:" + threadMsg);

}

else

{

Console.WriteLine("Forground Thread");

Console.WriteLine("My Threading method with:" + threadMsg);

}

}

We can check weather thread is background or not with Thread.CurrentThread.IsBackGround property.

Some time threading also overheads on processors, so need to take care while implementing threading as it distribute loads to different processor, more memory is required to manage resource.

Wise use of threading may improve application’s performance. It depends on requirement and application problem to use of Threading.

No comments: