public void Shutdown(int exitCode = 0) { if (_isShuttingDown) { throw new InvalidOperationException("Application is already shutting down."); } _exitCode = exitCode; _isShuttingDown = true; try { foreach (var w in Windows) { w.Close(); } var e = new ControlledApplicationLifetimeExitEventArgs(exitCode); Exit?.Invoke(this, e); _exitCode = e.ApplicationExitCode; } finally { _cts?.Cancel(); _cts = null; _isShuttingDown = false; } }
private bool DoShutdown(ShutdownRequestedEventArgs e, bool force = false, int exitCode = 0) { if (!force) { ShutdownRequested?.Invoke(this, e); if (e.Cancel) { return(false); } if (_isShuttingDown) { throw new InvalidOperationException("Application is already shutting down."); } } _exitCode = exitCode; _isShuttingDown = true; try { // When an OS shutdown request is received, try to close all non-owned windows. Windows can cancel // shutdown by setting e.Cancel = true in the Closing event. Owned windows will be shutdown by their // owners. foreach (var w in Windows) { if (w.Owner is null) { w.Close(); } } if (!force && Windows.Count > 0) { e.Cancel = true; return(false); } var args = new ControlledApplicationLifetimeExitEventArgs(exitCode); Exit?.Invoke(this, args); _exitCode = args.ApplicationExitCode; } finally { _cts?.Cancel(); _cts = null; _isShuttingDown = false; } return(true); }