2

我有这个

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()调用,直到调用弹出窗口并在处理程序中返回Resultfrom args。我不知道如何做到这一点,甚至不知道如何寻找它。谁能把我引向正确的方向?谢谢

4

2 回答 2

3

您想使用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();
}
于 2012-04-04T03:45:30.540 回答
0

这是粗略的,但应该给出一般的想法

public Result SomeMethod()
{
    Popup popup = new Popup();
    bool called = false;
    Result result = null;
    popup.Closed += (args) => {
        called = true;
        result = args.Result;
    }
    while(!called) ;
    return result;
}
于 2012-04-04T03:38:44.013 回答