[英]Pause and Resume the execution in WPF
Is there any way to pause and resume the execution in WPF App [especially in ViewModel class]? 有什么方法可以暂停和恢复WPF App中的执行(尤其是在ViewModel类中)? I have tried with Auto and ManualResetEvent class.
我尝试了Auto和ManualResetEvent类。 But it is not pausing at the point where I want to pause.
但是在我要暂停的地方并没有暂停。 The waitone method is not pausing the execution.
waitone方法不会暂停执行。
In my viewmodel, i have a method which calls the web service. 在我的视图模型中,我有一个调用Web服务的方法。 The result of the web service will be coming in another method [ie, the call back method].
Web服务的结果将以另一种方法[即回调方法]出现。 After getting the result from the web service, i want to continue the execution.
从Web服务获取结果后,我想继续执行。
public void Method1()
{
for(int i=0; i<5; i++)
{
// Call the web service. No waiting for the result.
// Block the execution here....
}
}
public void CallBackMethod(int serviceResult)
{
// After getting the result...
// I want to continue with Method1...
}
Is there any way to do it in WPF ? 在WPF中有什么办法吗?
You're talking about a ManualResetEvent
: 您在谈论
ManualResetEvent
:
private ManualResetEvent _reset;
public void Method1()
{
_reset = new ManualResetEvent(true);
for(int i=0; i<5; i++)
{
// Call the web service.
// WaitOne blocks the current thread
_reset.WaitOne();
}
}
public void CallBackMethod(int serviceResult)
{
// After getting the result...
// Set allows waiting threads to continue
_reset.Set();
}
But why do you need to do this in a loop? 但是,为什么需要循环执行此操作? Simply run the service again when the callback method is called:
只需在调用回调方法时再次运行服务:
int count =0;
const int MAX_CALLS = 5;
public void RunService()
{
//do service stuff
}
public void CallBackMethod(int serviceResult)
{
if (count++ < MAX_CALLS)
RunService ();
}
public static void Main (string[] args)
{
RunService ();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.