How to "wait" for another window to close

I have a lot of code that looks like this:

MyWindow window = new MyWindow(someParam, callingWindow)

With a MyWindow class containing something like this:

public MyWindow.Processing()
{
`  // Do processing
   ...
   callingWindow.RefreshListBox(); // Listbox for example
}

Is there a built-in way to wait for a window to close. I would like to write something like this in the callWindow class:

await mywindowInstanceCloseEvent; 
RefreshListBoxWhenWindowIsClosed();

I wonder if there is an easy way to “pause” a method (whether it is asynchronous or not) until a specific window is closed.

+4
source share
2 answers

It depends on your needs. If you need to show the window as a modal dialog, use Window.ShowDialog. Otherwise, you can use the event Window.Closed.

, @Athari.

+3

TaskCompletionSource Closed. , :

private Task ShowPopup<TPopup> (TPopup popup)
    where TPopup : Window
{
    var task = new TaskCompletionSource<object>();
    popup.Owner = Application.Current.MainWindow;
    popup.Closed += (s, a) => task.SetResult(null);
    popup.Show();
    popup.Focus();
    return task.Task;
}

:

await ShowPopup(new MyWindow());

Show ShowDialog, , .

, , TaskCompletionSource<Result> .

+11

All Articles