Thread Synchronisation in .Net using C#

This code demonstrate how to synchronise threads in .NET using C#



using System.Threading;
class Bank
{

Mutex m=new Mutex();
public void withdraw()
{

m.WaitOne();//this method will allow only one thread at a time
MessageBox.Show("From withdraw");
m.ReleaseMutex();//this methos is releasing executed thread features

}
}
private void button1_Click(Object ender,EventArgs e)
{
Bank obj=new Bank();
ThreadStart ts1=new ThreadStart(obj.withdraw);
Thread t1=new Thread(ts1);
Thread t2=new Thread(ts1);
t1.Start();
t2.Start();
}


Regards
S.S.Bajoria


Comments

Author: AmitChoudhary10 Sep 2008 Member Level: Bronze   Points : 2

Hi,
Where is ts2 declared ?
Have't you execucuted code without posting it ?
Pls run at least once before u post it on live ,
Even after declaration code will give error on

ThreadStart ts1=new ThreadStart(obj.withdraw());

don't simply put garbage for any body..

Thanks,
Amit Choudhary

Author: Bunty11 Sep 2008 Member Level: Gold   Points : 1

Hi,

Sorry to all of you.Now i corrected the code and also execute it its running.

I really apololizes for that.

Regards
S.S.Bajoria



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: