I have the following problem: I need to bind an asynchronous method to the Window.Closing event. Since the Closing event expects a CancelEventHandler with void signature, my WindowClosing method can’t be of type Task. So I have this:
public void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
DeinitializeThisFirst();
DeinitializeThisAfterwards();
}
For hardware reasons, DeinitializeThisFirst() must finish before DeinitializeThisAfterwards() is called. But DeinitializeThisFirst() is an awaitable Task. Since I can’t change the signature of Window_Closing to be a Task, I can’t do this:
public async Task Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
await DeinitializeThisFirst();
DeinitializeThisAfterwards();
}
And since I am on the UI thread, this will cause a deadlock:
public void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
DeinitializeThisFirst().ConfigureAwait(false).GetAwaiter().GetResult();
DeinitializeThisAfterwards();
}
Executing it in a separate Task will cause DeinitializeThisAfterwards() to be executed prematurely:
public void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Task.Run(async () => await DeinitializeThisFirst());
DeinitializeThisAfterwards();
}
And wrapping it in ContinueWith() will prevent both from being executed since the window is closed immediately:
public void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
DeinitializeThisFirst().ContinueWith(_ =>
{
DeinitializeThisAfterwards();
});
}
I am at a loss here. Can anyone help?