我正在尝试实施AutoResetEvent
. 为此,我使用了一个非常简单的类:
public class MyThreadTest
{
static readonly AutoResetEvent thread1Step = new AutoResetEvent(false);
static readonly AutoResetEvent thread2Step = new AutoResetEvent(false);
void DisplayThread1()
{
while (true)
{
Console.WriteLine("Display Thread 1");
Thread.Sleep(1000);
thread1Step.Set();
thread2Step.WaitOne();
}
}
void DisplayThread2()
{
while (true)
{
Console.WriteLine("Display Thread 2");
Thread.Sleep(1000);
thread2Step.Set();
thread1Step.WaitOne();
}
}
void CreateThreads()
{
// construct two threads for our demonstration;
Thread thread1 = new Thread(new ThreadStart(DisplayThread1));
Thread thread2 = new Thread(new ThreadStart(DisplayThread2));
// start them
thread1.Start();
thread2.Start();
}
public static void Main()
{
MyThreadTest StartMultiThreads = new MyThreadTest();
StartMultiThreads.CreateThreads();
}
}
但这不起作用。用法似乎非常简单,所以如果有人能够告诉我哪里出了问题以及我在这里实现的逻辑问题出在哪里,我将不胜感激。