Mega Code Archive

 
Categories / C# by API / System Threading
 

Mutex WaitOne()

using System; using System.Threading; public class MainClass  {   private static int Runs = 0;   static Mutex mtx;   public static void CountUp()    {     while (Runs < 10)     {       mtx.WaitOne();       int Temp = Runs;       Temp++;       Console.WriteLine(Thread.CurrentThread.Name + " " + Temp);       Thread.Sleep(1000);       Runs = Temp;       mtx.ReleaseMutex();     }    }   public static void Main()    {     mtx = new Mutex(false, "RunsMutex");     Thread t2 = new Thread(new ThreadStart(CountUp));     t2.Name = "t2";     Thread t3 = new Thread(new ThreadStart(CountUp));     t3.Name = "t3";     t2.Start();     t3.Start();   } }