I have a window displaying service, with a CloseWindow method that is called by the view. I want to create a blocking method in my calling code. So I can block while a window pops up and to allow outputs to come back from the window.
Is this use of Manual Reset acceptable? Are there any technical or design problems with it or with the way I mix it with TPL?
Here is that service
private readonly ManualResetEvent closedEvent = new ManualResetEvent(true);
public void DisplayWindow(){
window = new MyWindow();
}
public void CloseWindow() {
window.Close();
closedEvent.Set();
}
//new
public async Task WaitClosed()
{
await Task.Run(() => this.closedEvent.WaitOne());
}
here is some code that calls it.
public void DisplayWindow(string content, string title)
{
dialogservice.DisplayWindow();
}
public async Task DisplayWindowAsync(string content, string title)
{
dialogservice.DisplayWindow();
await dialogservice.WaitClosed();
}
It looks like it could be done more simply and without the hung thread:
private readonly TaskCompletionSource<bool> windowClosed
= new TaskCompletionSource<bool>();
public Task WindowClosed { get { return windowClosed.Task; } }
public void CloseWindow() {
window.Close();
windowClosed.TrySetResult(true);
}
with:
await dialogservice.WindowClosed;