我有这个
public Result SomeMethod()
{
Popup popup = new Popup();
popup.Closed += PopupClosedHandler;
popup.ShowPopup();
// have to return the result from handler.
}
void PopupClosedHandler(EventArgs<PopupEventArgs> args)
{
Result result = args.Result;
}
我必须阻止 SomeMethod()
调用,直到调用弹出窗口并从处理程序中的 args
返回 Result
。我不知道该怎么做,甚至不知道如何寻找它。谁能引导我走向正确的方向?谢谢
最佳答案
您想使用 EventWaitHandle .
public Result SomeMethod()
{
_doneHandler = new EventWaitHandle(false, EventResetMode.ManualReset);
Popup popup = new Popup();
popup.Closed += PopupClosedHandler;
popup.ShowPopup();
// This will wait until it is SET. You can pass a TimeSpan
// so that you do not wait forever.
_doneHandler.WaitOne();
// Other stuff after the 'block'
}
private EventWaitHandle _doneHandler;
void PopupClosedHandler(EventArgs<PopupEventArgs> args)
{
Result result = args.Result;
_doneHandler.Set();
}
关于C# 异步逻辑到同步调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10004551/