Skip to content

Commit

Permalink
#585 Handle async close in WindowManager
Browse files Browse the repository at this point in the history
  • Loading branch information
Nigel Sampson committed Mar 31, 2019
1 parent d0fd1d8 commit 72bd9d3
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/Caliburn.Micro.Platform/Platforms/net46/WindowManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ private class WindowConductor
{
private bool deactivatingFromView;
private bool deactivateFromViewModel;
private bool actuallyClosing;
private readonly Window view;
private readonly object model;

Expand Down Expand Up @@ -382,7 +383,9 @@ private void Deactivated(object sender, DeactivationEventArgs e)
}

deactivateFromViewModel = true;
actuallyClosing = true;
view.Close();
actuallyClosing = false;
deactivateFromViewModel = false;
}

Expand All @@ -393,11 +396,26 @@ private async void Closing(object sender, CancelEventArgs e)
return;
}

var guard = (IGuardClose) model;
var guard = (IGuardClose)model;

if (actuallyClosing)
{
actuallyClosing = false;
return;
}

var cachedDialogResult = view.DialogResult;

e.Cancel = true;

var canClose = await guard.CanCloseAsync(CancellationToken.None);

e.Cancel = !canClose;
if (canClose)
{
actuallyClosing = true;
view.DialogResult = cachedDialogResult;

This comment has been minimized.

Copy link
@markgould

markgould Apr 1, 2019

Contributor

This causes an error when closing the main window of the app (Or I assume any window that is not a dialog)

System.InvalidOperationException: 'DialogResult can be set only after Window is created and shown as dialog.'

This comment has been minimized.

Copy link
@nigel-sampson

nigel-sampson Apr 2, 2019

Contributor

Thanks for the catch, not sure how I missed that.

view.Close();
}
}
}
}
Expand Down

0 comments on commit 72bd9d3

Please sign in to comment.