[英]Suspend Main Thread, Execute some Callbacks, and then resume Main Thread
I need to do somethings, execute some callbacks events, and then resume my routine.我需要做一些事情,执行一些回调事件,然后恢复我的例程。 I'm using a Win Forms Application... I write you a little example.
我正在使用 Win Forms 应用程序...我给你写一个小例子。
void Do() // On main Thread
{
DoPartOne();
// Her i want to execute the callbacks events... and then execute
//parttwo
DoPartTwo();
}
void DoPartOne() // during this method i send commands to my connected service
{
StaticClass.Property = null;
}
void DoPartTwo()
{
StaticClass.Property = something...
}
void MyCallBackOnEvent(MyEvent)
{
if(StaticClass.Property == null)
DoThis();
else
DoThat();
}
During Party One my Client send a command to my connected Service and it response me with some variable numbers of events.... I need to execute callbacks first of my PartTwo() method, but i fix that callback execute it when i finish the Do() method.在第一方期间,我的客户向我连接的服务发送命令,它用一些可变数量的事件响应我....我需要首先执行我的 PartTwo() 方法的回调,但是我修复了该回调,当我完成做()方法。 Do you suggest me any other implementations?
你有什么其他的实现建议吗?
Do your action, then wait for a variable to become true:执行您的操作,然后等待变量变为真:
while(!workIsDone)
{
Thread.sleep(100);
}
Then in your event, set the variable in your event, like so:然后在您的事件中,在您的事件中设置变量,如下所示:
void MyCallBackOnEvent(MyEvent)
{
// Event has fired, so we can set workIsDone to true and then the main thread will stop the while loop, and continue normally.
workIsDone = true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.