private AutoResetEvent exitevent; private void Processa () { while (true ) {Console.WriteLine ( "
do some thing " if (Exitevent.waitone (waitTime)) {break ; }} Console.WriteLine ( " exit " );} Exitevent.set ();
Private void Processb () { while (true) { Console.WriteLine ('do Some thing"); Thread.Sleep (WaitTime) }}
About the threading small model, there are two processing functions, one is a simple sleep wait, one is the semaphore, but there is a flaw, that is, whether it is executed at the time (do some thing) cannot exit. The signal volume is better, can control the exit when waiting, sleep words can only throw exception to exit (or set flag in while, but same).
Public New Semaphoreslim (ten); // concurrency limit is ten
In a multithreaded model, you can control the number of threads to control concurrency.
= = {Console.WriteLine ("dosome thing"); Semaphoreslim.release ();});
Suitable for a large number of threads to do the same thing to control concurrency, pretty useful
Multithreading, threading control concurrency