.NET thread synchronization (3)

zhaozj2021-02-16  27

Another synchronization policy is hand-controlled technology, some of the SYSTEM.THREADING namespace can be used for hand-controlled classes. The ManualReveTevent class is used to make the thread in the waiting state, which has 2 states: True or No Signal (FALSE). There are also two important methods: reset () and set ().

The following code shows the usage of the reset () method:

Using system; using system.threading;

namespace ManualReset {class Reset {[STAThread] static void Main () {ManualResetEvent manRE; manRE = new ManualResetEvent (true); // assigns semaphore bool state = manRE.WaitOne (1000, true); Console.WriteLine ( "ManualResetEvent After first waitone " state);

ManRe.Reset (); // Set the ManualReveTevent status is no semaphore (5000, true); console.writeline ("ManualRestEventAfter Second Waitone" State);}}}

operation result:

The following code shows the usage of the SET () method:

using System; using System.Threading; namespace ManualSet {class Set {[STAThread] static void Main (string [] args) {ManualResetEvent manRE; manRE = new ManualResetEvent (false); Console.WriteLine ( "Before waitone"); bool state = Manre.waitone (5000, true); Console.Writeline ("ManualRestEvent After First Waitone State);

ManRe.set (); // set its status to have signal volume thread.sleep (3000); state = manre.waitone (5000, true); console.writeline ("ManualRestEventAfter Second Waitone State);}} }

operation result:

Lsqxw@hotmail.com

转载请注明原文地址:https://www.9cbs.com/read-27922.html

New Post(0)