Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C#
Article

Asynchronous Threading

Rate me:
Please Sign up or sign in to vote.
1.16/5 (26 votes)
5 Dec 2005 64.4K   1.2K   12   9
Asynchronous Threading Using C#

Sample Image - Threading.jpg

Introduction

This code demonstrate how easily one can create a asynchronouse processes using .net threading.

Why Use Asynchronouse Threading:

Let' say you have a budle of requests to save data then you need to use threading
other wise system will wait for first request to be completed and then awoke function  for sencond request.

By using threading always a new instance of of your function will be created and many instances will be running togeather, and data will be saved parrally from application end.

What you have to do:

Create a function that will be called rapidly.
Suppose 

C#
void 
SaveUser(int id,string name){...} 

Now create a function that will create thread and add this function to it...

C#
void dosave(id,name){<P></P>
<P>t=<FONT color=#0000ff>new</FONT> Thread (<FONT color=#0000ff>new</FONT> 
ThreadStart(SaveUser(id,name)));</P>
<P>}</P>

On each request of save data call above function...

C#
dosave(1,"jhon smith");<P></P>

 

 

What you have to include to use threading

You have to include threading name space in order to use the threading class.

using System.Threading;

 

 

Form1.cs

<FONT color=#0000ff><P></FONT> </P><P><FONT color=#0000ff>private</FONT> <FONT color=#0000ff>void</FONT> Form1_Load(<FONT color=#0000ff>object</FONT> sender, System.EventArgs e)</P><P>{</P><P>//to track maximum request count</P><P></P><P></P><P></P><P>count=0;</P><P>//set text for button 1</P><P>button5.Text ="Start Thread 1 ";</P><P></P><P>}</P><P><FONT color=#0000ff>private</FONT> <FONT color=#0000ff>void</FONT> button5_Click(<FONT color=#0000ff>object</FONT> sender, System.EventArgs e)</P><P>{</P><P> </P><P>//incremnet in count and maximun count can be 4</P><P></P><P></P><P></P><P><FONT color=#0000ff>if</FONT> ((count+1)<=4){</P><P></P><P>count++;</P><P></P><P></P><P></P><P><FONT color=#008000>//create a new theread that will start function "StartThread"</P></FONT><P>t=<FONT color=#0000ff>new</FONT> Thread (<FONT color=#0000ff>new</FONT> ThreadStart(StartThread));</P><P><FONT color=#008000>//add thread to a hash table so that you can handle each instance of the thread.</FONT></P><P>threadHolder.Add(threadCount++,t);</P><P><FONT color=#008000>//Give some name to thread</FONT></P><P>t.Name = "Thread ID: "+threadCount;</P><P>t.IsBackground = <FONT color=#0000ff>true</FONT>;</P><P><FONT color=#008000>//Start thread</FONT></P><P>t.Start();</P><P></P><P></P><P></P><P></P><P></P><P>}</P><P></P><P><FONT color=#0000ff>if</FONT> ((count+1)<5)</P><P>{button5.Text ="Start Thread " + ((count+1)) ;}</P><P><FONT color=#0000ff>else</P></FONT><P>{ </P><P>button5.Text ="All Threads Started";</P><P>button5.Enabled =<FONT color=#0000ff>false</FONT>;</P><P>}</P><P> </P><P>}</P><P> </P><P> </P><P> </P><P><FONT color=#0000ff>private</FONT> <FONT color=#0000ff>void</FONT> StartThread()</P><P>{</P><P><FONT color=#008000> </FONT></P><FONT color=#008000><FONT color=#008000><P><FONT color=#008000>//create instance form to show repation of thread</P></FONT></FONT><P><FONT color=#008000> </P></FONT></FONT><P>Form2 frm=<FONT color=#0000ff>new</FONT> Form2();</P><P>frm.Show ();</P><P></P><P><FONT color=#0000ff>int</FONT> localCopy=count; </P><P><FONT color=#0000ff>int</FONT> WR=0;</P><P><FONT color=#0000ff>string</FONT> sW="";</P><P><FONT color=#008000>//run always </FONT></P><P><FONT color=#0000ff>while</FONT>(<FONT color=#0000ff>true</FONT>)</P><P>{</P><P></P><P>WR++;</P><P>sW=WR.ToString(); </P><P>frm.Text="Thread " + localCopy + " (Repeat=" + sW + ")" ; </P><P></P><P><FONT color=#008000>//give some break</FONT></P><P>Thread.Sleep(100); </P><P><FONT color=#0000ff>try</P></FONT><P>{ </P><P> </P><P><FONT color=#008000>//change back colors to show thread is running</FONT><P></P><FONT color=#0000ff>switch</FONT> (localCopy)<P></P><P></P><P>{</P><P><FONT color=#0000ff>case</FONT> 1:</P><P></P><P><FONT color=#0000ff>if</FONT> (<FONT color=#0000ff>this</FONT>.button1.BackColor==Color.Red ) </P><P><FONT color=#0000ff>this</FONT>.button1.BackColor=Color.Green;</P><P><FONT color=#0000ff>else</P></FONT><P><FONT color=#0000ff>this</FONT>.button1.BackColor=Color.Red;</P><P></P><P> </P><P><FONT color=#0000ff>break</FONT>;</P><P><FONT color=#0000ff>case</FONT> 2:</P><P></P><P><FONT color=#0000ff>if</FONT> (<FONT color=#0000ff>this</FONT>.button2.BackColor==Color.Red ) </P><P><FONT color=#0000ff>this</FONT>.button2.BackColor=Color.Green;</P><P><FONT color=#0000ff>else</P></FONT><P><FONT color=#0000ff>this</FONT>.button2.BackColor=Color.Red;</P><P><FONT color=#0000ff>break</FONT>;</P><P><FONT color=#0000ff>case</FONT> 3:</P><P></P><P><FONT color=#0000ff>if</FONT> (<FONT color=#0000ff>this</FONT>.button3.BackColor==Color.Red ) </P><P><FONT color=#0000ff>this</FONT>.button3.BackColor=Color.Green;</P><P><FONT color=#0000ff>else</P></FONT><P><FONT color=#0000ff>this</FONT>.button3.BackColor=Color.Red;</P><P><FONT color=#0000ff>break</FONT>;</P><P><FONT color=#0000ff>case</FONT> 4:</P><P></P><P><FONT color=#0000ff>if</FONT> (<FONT color=#0000ff>this</FONT>.button4.BackColor==Color.Red ) </P><P><FONT color=#0000ff>this</FONT>.button4.BackColor=Color.Green;</P><P><FONT color=#0000ff>else</P></FONT><P><FONT color=#0000ff>this</FONT>.button4.BackColor=Color.Red;</P><P><FONT color=#0000ff>break</FONT>;</P><P>}</P><P> </P><P></P><P></P><P></P><P></P><P>}</P><P></P><P><FONT color=#0000ff>catch</FONT>(Exception e1)</P><P>{</P><P>Console.WriteLine("Exception Form1 loop >> "+e1);</P><P><FONT color=#0000ff>break</FONT>; </P><P>}</P><P>}</P><P>}</P><P></P><P></P><P> </P><P>}</P><P> </P><P>}</P>

Form2.cs

This form shows the no of time for which a single process is repeating.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Business Analyst Valentia Technologies
Ireland Ireland
Mubi
^^^^^^^^^^^^^^^^^^^^^^^
www.mrmubi.com

Comments and Discussions

 
Generalouch! Pin
mrwizzard21-Jul-07 18:55
mrwizzard21-Jul-07 18:55 
GeneralThe War Against Terrorism - PMS - Dog_Spawn - Jane Cooper - +44 1536 358530 Pin
Anonymous27-Apr-04 12:15
Anonymous27-Apr-04 12:15 
GeneralRe: The War Against Terrorism - PMS - Dog_Spawn - Jane Cooper - +44 1536 358530 Pin
anonymous427-Apr-04 14:58
anonymous427-Apr-04 14:58 
GeneralGood, but... Pin
mirano20-Apr-04 0:30
mirano20-Apr-04 0:30 
GeneralRe: Good, but... Pin
dog_spawn23-Apr-04 8:45
dog_spawn23-Apr-04 8:45 
GeneralNon-article! Pin
Dr Herbie19-Apr-04 21:57
Dr Herbie19-Apr-04 21:57 
GeneralRe: Non-article! Pin
Mubi | www.mrmubi.com20-Apr-04 0:15
professionalMubi | www.mrmubi.com20-Apr-04 0:15 
GeneralRe: Non-article! Pin
Andrew McCarter20-Apr-04 5:03
Andrew McCarter20-Apr-04 5:03 
I don't think anyone here has any trouble understanding the "code". Clearly you have a problem understanding what "article" means.
GeneralIdiot Pin
dog_spawn20-Apr-04 8:02
dog_spawn20-Apr-04 8:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.