.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Threading >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Mutex -- a sample code

Posted By:nishithraj       Posted Date: November 27, 2009    Points: 10    Category: Threading    URL: http://www.dotnetspark.com  
 

Mutex - a sample code.


If there are multiple threads accessing the same resource, there might be a chance for dead locking. To avoid this we can implement synchronization of threads. Mutex is a synchronization method available in .net. Here is a sample for mutex.


//Creating mutex object
Mutex objmutex = new Mutex(false);
    public  void callMe()
    {
//mutex in action - mutex locks exclusively the first thread.
         objmutex.WaitOne();
int i;
String tname;
Thread t = Thread.CurrentThread;
tname =t.Name;
for (i=0;i<= 5;i++){
Thread.Sleep(1000);
System.Console.WriteLine( tname  + "  : " + i );
}
objmutex.Close();
    }

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend