Multithreading with an Example of .NET Application
page 1 of 9
Published: 11 Oct 2006
Abstract
In this article Sandeep discusses the basics of multithreading and how it could be effective over a single thread application. It also includes the advantages and disadvantages of multithreading. The article includes the complete .NET project for creating a simple multithreaded application.
by Sandeep Acharya
Feedback
Average Rating: 
Views (Total / Last 10 Days): 154364/ 586

Introduction

Threads are also known as lightweight processes.  However, if we go into depth then we would know that a thread is not actually a process; rather it provides ways for executing different parts of a program.  Now let us discuss what it actually means by multithreading.  Multithreading (as the name suggests multi+threading) is nothing but an efficient execution of multiple threads at a time to enhance the performance of the application.  For example, we are doing a file copy operation with a status bar on UI indicating the completion percentage.  Here we need to keep track of how much file size is copied and at the same time we also need to advance the progress bar accordingly.  This can not be done efficiently in a single thread and you have to use multiple threads.

The above example shows just one instance where we are forced to use multithreading.  However, when we are not forced we can also use this for the betterment of the application performance. And of course, all this depends on how effectively the thread is implemented in an application.  Most of the developers do not use multithreaded applications and continue with a single thread.  However, the efficient use of threads can give birth to a highly powerful application.


View Entire Article

User Comments

Title: Improve Basic Multithreading Code   
Name: Abhijeet Rana
Date: 5/11/2012 2:08:03 AM
Comment:
Hi All,
If you copy and pest above code as usual in .Net the you will get error saying "Cross-thread operation not valid: Control 'cmb1' accessed from a thread other than the thread it was created on." as i did that so i got this error
Error occurred because main thread is running for form and listbox control is not accessible at this point
So i have modified this code as below-

Imports System.Threading

Public Class Form1
Inherits System.Windows.Forms.Form

Dim th1 As Thread
Dim th2 As Thread

'Declaring Delegate here
Private Delegate Sub proc1Delegate
Private Delegate Sub proc2Delegate

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
th1 = New Thread(AddressOf proc1)
th2 = New Thread(AddressOf proc2)

th1.Start()
th2.Start()
End Sub

Sub proc1()
'Calling Dalegate here and passing addressof Proc1
Dim DelProc1 As proc1Delegate(AddressOf proc1)
Dim iCount As Integer

If cmb1.InVokeRequired Then
cmb1.InVoke(DelProc2)
Else
For iCount = 1 To 10
cmb1.Items.Add(iCount)
Next
End If

End Sub

Sub proc2()
'Calling Dalegate here and passing addressof Proc2
Dim DelProc2 As proc2Delegate(AddressOf proc2)
Dim iCount As Integer

If cmb2.InVokeRequired Then
cmb2.InVoke(DelProc2)
Else
For iCount = 11 To 20
cmb2.Items.Add(iCount)
Next
End If

End Sub
End Class


* Correct me if i wrong
Title: eagle   
Name: desert eagle
Date: 10/25/2011 7:18:46 AM
Comment:
desert eagle want to know how to work through multiThreading
Title: Good   
Name: Farogh Ahmed
Date: 5/31/2011 10:38:57 AM
Comment:
Very good explanation with example.

Thanks,
Title: it is nice article for beginner   
Name: Anjanareddy
Date: 11/18/2010 6:15:24 AM
Comment:
It is very useful for beginner.i tried ur program but when i give breakepoint that time it work perfectly.is it need brake point while debugging ?
nice article.Thank You
Title: How to overcome- "Cross Thread Operation" problem   
Name: Balakrishnan.S
Date: 7/23/2010 2:33:58 PM
Comment:
When running the downloaded appliation Showing error "Cross-thread operation not valid: Control 'cmb1' accessed from a thread other than the thread it was created on."

Please give me the solution......
Title: article is really good   
Name: neha
Date: 7/17/2010 1:56:12 AM
Comment:
information regarding multithread concept is very good n easy to understand. Its very helpful for the beginners
Title: ss   
Name: ss
Date: 7/8/2010 8:19:37 AM
Comment:
When running the downloaded appliation Showing error "Cross-thread operation not valid: Control 'cmb1' accessed from a thread other than the thread it was created on."
Title: Thank you   
Name: Bilal
Date: 5/16/2010 3:40:40 AM
Comment:
many thanks
Title: multitreading   
Name: Amin Rasmin
Date: 3/30/2010 9:25:29 PM
Comment:
thank u for ur information, it's help me, i'l try to use it.
Title: It doesn't work   
Name: Anonymous
Date: 3/19/2010 5:34:30 AM
Comment:
This article is good and easy to understand, but the code just doesn't work for me. I have created a C# web app and filling listboxes. Below is my code.

protected void Page_Load(object sender, EventArgs e)
{
Thread th1;
Thread th2;

th1 = new Thread(Proc1);
th2 = new Thread(Proc2);

th2.Start();
th1.Start();

}
private void Proc1()
{
for (int i = 1; i <= 10; i++)
{
ListBox1.Items.Add(i.ToString());
//Thread.Sleep(100);
}
}
private void Proc2()
{
for (int i = 11; i <= 20; i++)
{
ListBox2.Items.Add(i.ToString());
//Thread.Sleep(10);
}
}

Some times it works and only populates the ListBox 2 as I have called thread 2 on top of thread 1. If I call thread 1 before thread 2 it will populate only List Box 1.

Even with the Thread.Sleep uncommented the code wont work.

Can anyone please tell me what is wrong with this piece of code as I am totally clueless.
Title: example of multithreading in microsoft word   
Name: mariyan
Date: 10/8/2009 5:38:37 AM
Comment:
can you give some example of multi-threading in microsoft word
. because i not yet to understand about it process when microsoft office word is runing
Title: I have this same problem like Eman mentioned here   
Name: kumar
Date: 7/2/2009 1:16:00 PM
Comment:
I dont see second combo populating until I debug the code step by step using break points. Am I doing something wrong plz clarify?
Title: Best Article to Start with   
Name: Geetha
Date: 5/28/2009 3:45:53 AM
Comment:
Hi Sandeep,

The article is really nic to understand basic of mutithreading.
Its useful for beginners.
Title: nice   
Name: Dima
Date: 4/12/2009 9:07:59 PM
Comment:
very nice ............
Title: Nice Article   
Name: Jignesh Patel
Date: 8/19/2008 9:54:41 PM
Comment:
Very nice ..easy to understand for beginners..
Thanks..
Title: RE: It won't work   
Name: Ibrahim mohsen
Date: 5/22/2008 9:48:07 AM
Comment:
you shouldn't access any controls inside threads

you may make flag and in the end of thread method raise this flag and make timer to check if this flag is raised.

if it is raised access what you need of controls
Title: It won't work   
Name: Abhishek
Date: 4/28/2008 3:58:57 PM
Comment:
Cross-thread operation not valid: Control 'comboBox2' accessed from a thread other than the thread it was created on.

I'm getting this error. I'm not able to do the multi-threading in any way on windows systems. Anybody have any working sample/example?
Title: Shallow Coverage   
Name: ApuDeb
Date: 4/26/2008 2:17:48 AM
Comment:
Hello Sandeep, I found the coverage of the topic shallow and found a lot of room to improve it.
Title: kill a thread that is spawned by another process   
Name: Mia
Date: 1/24/2008 3:44:06 PM
Comment:
I have an app that starts a thread. The threads opens a new aspx page and runs a process to fill the page with data.
The currentThread in the new aspx page is the currentThreadID of the app and not the threadID of the new thread. How can I kill the new thread from the new aspx page. if I just kill the currentThread, it will shut down the whole app.
Title: Very Simple   
Name: Abdulla Hussein
Date: 9/28/2007 5:58:50 PM
Comment:
Very Simple Articals , and it is help me to understanding MultiThreading in a nice way . Thanks :)
Title: Multithread   
Name: Rajesh
Date: 4/4/2007 2:18:12 AM
Comment:
Nice article
Title: need help   
Name: Dhananjay k
Date: 3/21/2007 8:32:20 AM
Comment:
hi,i am trying to make online multiplayer game(card game like poker)now in current state the game is almost completed and in LAN it working fine. but whn i try to use online database(ie fm remote db server) the game sleeps can anyone suggest me hw can i solve the prob. ( using vb.net and sql server 2000)
Title: possibility   
Name: balaji
Date: 3/16/2007 7:22:59 AM
Comment:
i want to move multiple objects and , iwant to see at run time those actions in asp.net 2005 version
Title: answers to questions   
Name: KraGiE
Date: 2/13/2007 6:44:19 AM
Comment:
First off, this is a pretty good beginners look at multithreading in .net. Would be nice to see more low level info, and practical do's for threading.

1) annie xavier
if you're only using one thread, it would probably be better not to sleep it. However, it all depends on the scenario itself. Can you use a timer and/or an event callback that will trigger your working thread to start?

2) Eman
Typical problem with debugging multithreaded applications. Get used to utilizing thread names, and conditional breakpoints. you can also use windbg if you've learned how already.
Title: Doubt   
Name: annie xavier
Date: 2/7/2007 4:52:49 AM
Comment:
i am not using threads in my application. Now in my application i need to wait for soemtime even before i execute a piece of code. Is it ok if i specify thread.sleep in the main thread itself?
Title: there is a proplem   
Name: Eman
Date: 1/28/2007 5:39:24 AM
Comment:
i have a proplem that wehen i run a project motr than once thread dosen't work unless i debug it line by line by using
breakpoint
why????????????????????
please give me answer by mail orin this page quickly
thanks
Title: Hi   
Name: Padmapolisetti
Date: 1/11/2007 4:35:46 AM
Comment:
Hello,
Ur material helped me a lot in learning about the threads.
can u provide some more material on threads in depth.
Title: hello   
Name: hassan hayek
Date: 11/4/2006 2:27:32 PM
Comment:
thanks
Title: jj   
Name: ashish
Date: 10/23/2006 11:14:19 PM
Comment:
ur material is very good n m very satisfied n got new things from ur sites everything is worhty for me

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2014 ASPAlliance.com  |  Page Processed at 10/24/2014 10:45:24 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search