[英]waking up a thread from sleep in response to an event c#
Lets suppose i have 3 threads A, B and C 假设我有3个线程A,B和C
A starts B whereas C is an independent thread with no relation to either A or B. So now A calls B does some work and sleeps waiting for a signal from B or C to wakeup and start doing work again. A启动B,而C是与A或B无关的独立线程。因此,现在A调用B进行一些工作,然后睡眠,等待B或C发出的信号唤醒并重新开始工作。
How can this cross thread event handling be achieved in C# ? 如何在C#中实现这种跨线程事件处理?
Use a ManualResetEvent
or an AutoResetEvent
(based on the particular semantics you desire; without more info it's hard to say which is more appropriate). 使用
ManualResetEvent
或AutoResetEvent
(基于所需的特定语义;没有更多信息,很难说哪种更合适)。 Create the appropriate event object and share it with all three threads. 创建适当的事件对象并将其与所有三个线程共享。 Have thread
A
wait on the event. 让线程
A
等待事件。 Either B
or C
can then set the event to wake up thread A
. 然后,
B
或C
都可以将事件设置为唤醒线程A
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.