Click here to Skip to main content
Click here to Skip to main content
Go to top

Asynchronous Threading

, 5 Dec 2005
Rate this:
Please Sign up or sign in to vote.
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 

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

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

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...

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

Share

About the Author

Mubi | www.mrmubi.com
Business Analyst Valentia Technologies
Ireland Ireland
Mubi
^^^^^^^^^^^^^^^^^^^^^^^
www.mubshir.com
Follow on   Twitter

Comments and Discussions

 
Generalouch! Pinmembermrwizzard21-Jul-07 18:55 
GeneralThe War Against Terrorism - PMS - Dog_Spawn - Jane Cooper - +44 1536 358530 PinsussAnonymous27-Apr-04 12:15 
GeneralRe: The War Against Terrorism - PMS - Dog_Spawn - Jane Cooper - +44 1536 358530 PinsussAnon ... just like you27-Apr-04 14:58 
GeneralGood, but... Pinmembermirano20-Apr-04 0:30 
GeneralRe: Good, but... Pinmemberdog_spawn23-Apr-04 8:45 
GeneralNon-article! PinmemberDr Herbie19-Apr-04 21:57 
GeneralRe: Non-article! PinmemberMubashir Raza Ali Khan Balouch20-Apr-04 0:15 
GeneralRe: Non-article! PinmemberAndrew McCarter20-Apr-04 5:03 
GeneralIdiot Pinmemberdog_spawn20-Apr-04 8:02 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 5 Dec 2005
Article Copyright 2004 by Mubi | www.mrmubi.com
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid