class Program { static EventWaitHandle eHandle = new EventWaitHandle(false, EventResetMode.AutoReset,"LLLLL"); static void Main(string[] args) { Console.WriteLine("主动控制"); Thread.Sleep(3000); Start(); Thread.Sleep(3000); Start(); Thread.Sleep(3000); Start(); Thread.Sleep(3000); Start(); Thread.Sleep(3000); Start(); } static bool Start() { return eHandle.Set(); } }
class Program { static EventWaitHandle eHandle = new EventWaitHandle(false, EventResetMode.AutoReset, "LLLLL"); static void Main(string[] args) { Console.WriteLine("被控制"); Thread th = new Thread(DoMethod); th.Start(); Start(); // Start(); //Stop(); //Start(); Stop(); Console.ReadKey(); } static bool Stop() { return eHandle.WaitOne(); } static bool Start() { return eHandle.Set(); } static void DoMethod() { while (true) { Stop(); Console.WriteLine(" running"); System.Threading.Thread.Sleep(1000); } } }