8
के साथ दो धागे सिंक्रनाइज़ करना मैं AutoResetEvent
को लागू करने की कोशिश कर रहा हूं। इस उद्देश्य के लिए मैं एक बहुत ही सरल वर्ग का उपयोग करता हूं: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();
}
}
लेकिन यह काम नहीं कर रहा है। उपयोग बहुत सीधी-आगे प्रतीत होता है, इसलिए अगर मैं किसी को यह दिखाने में सक्षम हूं कि क्या गलत है और तर्क के साथ समस्या कहां है, तो मैं सराहना करता हूं।
कैसे यह नहीं है काम कर रहे? क्या होता है? – SLaks
वैसे जो कोड आपने दिया है वह भी संकलित नहीं होगा - आपने कभी '_stopThreads' घोषित नहीं किया है ... –
@ जॉन स्कीट मैं समस्या को अलग करने के लिए कोड बदलता हूं, अब यह तय हो गया है। – Leron