[英]ManualResetEvent (WaitOne and Set) repeating multiple times
Is it possible to repeat multiple times the ManualResetEvent? 是否可以重复多次ManualResetEvent?
Something like this: 像这样:
receivedDone.WaitOne();
//something here
receivedDone.Set(); //this go back to receivedDone.WaitOne()
//when executing the second time will loop the receivedDone.Set() and not returning
//again to receivedDone.WaitOne(); like I wanted.
So my question is: 所以我的问题是:
Is it possible to execute multiple times like a loop the same WaitOne(); 是否可以像同一个WaitOne()一样循环执行多次; and Set();?
和Set();?
EDIT: 编辑:
I have a button, when I click it run a function to start my tcpclient. 我有一个按钮,当我单击它时,运行一个函数来启动我的tcpclient。
After that I wait for some response from the server with the receivedDone.WaitOne();
在那之后,我用
receivedDone.WaitOne();
等待服务器的一些响应receivedDone.WaitOne();
when I receive the message on my buffer, it goes to receivedDone.Set();
当我在缓冲区上收到消息时,它将转到
receivedDone.Set();
. 。 This works 1 time, but I want to make it multiple times with the same WaitOne();
这工作1次,但我想使用相同的WaitOne()多次进行; and Set();
和Set();
Is this possible? 这可能吗?
As the name says, a ManualResetEvent
must be reset manually. 顾名思义,必须手动重置
ManualResetEvent
。 It is like a door. 就像一扇门。 It is initialized with
初始化为
ManualResetEvent ev = new ManualResetEvent(false); // The door is closed
or 要么
ManualResetEvent ev = new ManualResetEvent(true); // The door is open
A thread, which calls WaitOne
passes the door if is is open, otherwise waits at the door until it opens. 如果打开的话,调用
WaitOne
线程将通过门,否则将在门上等待直到打开。
A call of 的呼吁
ev.Set();
opens the door and a call of 打开门,
ev.Reset();
closes the door. 关上门。
As far as I understand your question, an AutoResetEvent
would help more. 据我了解您的问题,
AutoResetEvent
会提供更多帮助。 Or even better create a async function which proceed the TCP call and returns the result. 甚至更好地创建一个异步函数,该函数继续进行TCP调用并返回结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.