This article describes the usage of C# thread queues. Share it for your reference. The details are as follows:
using System; using ; using ; using ; namespace ThreadPro { class Program { static Mutex gM1; static Mutex gM2; const int ITERS = 100; static AutoResetEvent Event1 = new AutoResetEvent(false); static AutoResetEvent Event2 = new AutoResetEvent(false); static AutoResetEvent Event3 = new AutoResetEvent(false); static AutoResetEvent Event4 = new AutoResetEvent(false); static void Main(string[] args) { ("Mutex Sample "); //Create a Mutex object and name it MyMutex gM1 = new Mutex(true, "MyMutex"); //Create an unnamed Mutex object. gM2 = new Mutex(true); (" - Main Owns gM1 and gM2"); AutoResetEvent[] evs = new AutoResetEvent[4]; evs[0] = Event1; //Define the AutoResetEvent object for the subsequent threads t1, t2, t3, t4, and t4. evs[1] = Event2; Program tm = new Program(); Thread t1 = new Thread(new ThreadStart(tm.t1Start)); Thread t2 = new Thread(new ThreadStart(tm.t2Start)); Thread t3 = new Thread(new ThreadStart(tm.t3Start)); Thread t4 = new Thread(new ThreadStart(tm.t4Start)); ();// Use the() method to wait for all objects in a Mutex array to be released ();// Use the() method to wait for the release of gM1 ();// Use the() method to wait for any object in a Mutex array to be released ();// Use the() method to wait for the release of gM2 (2000); (" - Main releases gM1"); (); //The end condition of thread t2 and t3 is full (1000); (" - Main releases gM2"); (); //The end condition of thread t1 and t4 is satisfied //Waiting for all four threads to end (evs); (" Mutex Sample"); (); } public void t1Start() { ("Method 1 Run, (Mutex[])"); Mutex[] gMs = new Mutex[2]; gMs[0] = gM1;//Create a Mutex array as a parameter of the() method gMs[1] = gM2; (gMs);//Waiting for both gM1 and gM2 to be released (); //Fixed the last error (); //Fixed the last error (2000); ("Once the method is finished,WaitAll(Mutex[]) satisfied"); (); //The thread ends, set Event1 to signaled state } public void t2Start() { ("Method 2 run, ( )"); ();//Waiting for the release of gM1 (); //Fixed the last error ("Method 2 is finished, ( ) satisfied"); ();//The thread ends, set Event2 to signaled state } public void t3Start() { ("t3Start started, (Mutex[])"); Mutex[] gMs = new Mutex[2]; gMs[0] = gM1;//Create a Mutex array as a parameter of the() method gMs[1] = gM2; (gMs);//Waiting for any Mutex object in the array to be released (); //Fixed the last error ("t3Start finished, (Mutex[])"); ();//The thread ends, set Event3 to signal state } public void t4Start() { ("t4Start started, ( )"); ();//Waiting for gM2 to be released (); //Fixed the last error ("t4Start finished, ( )"); ();//The thread ends, set Event4 to signal state } } }
I hope this article will be helpful to everyone's C# programming.