Skip to content

Commit

Permalink
add TryShutdown method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Walmsley committed Jan 20, 2022
1 parent 643c748 commit ad2d024
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ private void HandleWindowClosed(Window window)

public void Shutdown(int exitCode = 0)
{
DoShutdown(new ShutdownRequestedEventArgs(), exitCode);
DoShutdown(new ShutdownRequestedEventArgs(), true, exitCode);
}

public bool TryShutdown(int exitCode = 0)
{
return DoShutdown(new ShutdownRequestedEventArgs(), false, exitCode);
}

public int Start(string[] args)
Expand Down Expand Up @@ -126,12 +131,12 @@ public void Dispose()
_activeLifetime = null;
}

private void DoShutdown(ShutdownRequestedEventArgs e, int exitCode = 0)
private bool DoShutdown(ShutdownRequestedEventArgs e, bool force = false, int exitCode = 0)
{
ShutdownRequested?.Invoke(this, e);

if (e.Cancel)
return;
false;

if (_isShuttingDown)
throw new InvalidOperationException("Application is already shutting down.");
Expand All @@ -155,7 +160,7 @@ private void DoShutdown(ShutdownRequestedEventArgs e, int exitCode = 0)
if (Windows.Count > 0)
{
e.Cancel = true;
return;
return false;
}

var e = new ControlledApplicationLifetimeExitEventArgs(exitCode);
Expand All @@ -168,6 +173,8 @@ private void DoShutdown(ShutdownRequestedEventArgs e, int exitCode = 0)
_cts = null;
_isShuttingDown = false;
}

return true;
}

private void OnShutdownRequested(object sender, ShutdownRequestedEventArgs e) => DoShutdown(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ namespace Avalonia.Controls.ApplicationLifetimes
/// </summary>
public interface IClassicDesktopStyleApplicationLifetime : IControlledApplicationLifetime
{
/// <summary>
/// Tries to Shutdown the application. <see cref="ShutdownRequested" /> event can be used to cancel the shutdown.
/// </summary>
/// <param name="exitCode">An integer exit code for an application. The default exit code is 0.</param>
bool TryShutdown(int exitCode = 0);

/// <summary>
/// Gets the arguments passed to the
/// <see cref="ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime{T}(T, string[], ShutdownMode)"/>
Expand Down

0 comments on commit ad2d024

Please sign in to comment.