Simplify and better comments
MODIFY
WalletWasabi.Gui/Program.cs
WalletWasabi.Gui/Program.cs
@@ -5,6 +5,7 @@ using Avalonia.Threading;
using AvalonStudio.Extensibility;
using AvalonStudio.Shell;
using AvalonStudio.Shell.Extensibility.Platforms;
+using Serilog.Configuration;
using Splat;
using System;
using System.IO;
@@ -67,22 +68,12 @@ namespace WalletWasabi.Gui
BuildAvaloniaApp().StartShellApp("Wasabi Wallet", AppMainAsync, args);
}
}
- catch (OperationCanceledException ex)
- {
- Logger.LogDebug(ex);
- }
catch (Exception ex)
{
appException = ex;
- Logger.LogCritical(ex);
-
- if (runGui)
- {
- CrashReporter.SetException(ex);
- }
}
- TerminateService.Terminate(appException);
+ TerminateAppAndHandleException(appException, runGui);
}
private static Global CreateGlobal()
@@ -131,21 +122,37 @@ namespace WalletWasabi.Gui
}
catch (Exception ex)
{
- var criticalException = ex is OperationCanceledException ? null : ex;
-
- // There is no other way to stop the creation of the WasabiWindow.
- TerminateService.Terminate(criticalException);
+ // There is no other way to stop the creation of the WasabiWindow, we have to exit the application here instead of return to Main.
+ TerminateAppAndHandleException(ex, true);
}
}
- private static async Task TerminateApplicationAsync(Exception? criticalException = null)
+ /// <summary>
+ /// This is a helper method until the creation of the window in AppMainAsync cannot be aborted without Environment.Exit().
+ /// </summary>
+ private static void TerminateAppAndHandleException(Exception? ex, bool runGui)
{
- if (criticalException is { })
+ if (ex is OperationCanceledException)
{
- Logger.LogCritical(criticalException);
- CrashReporter.SetException(criticalException);
+ Logger.LogDebug(ex);
+ }
+ else if (ex is { })
+ {
+ Logger.LogCritical(ex);
+ if (runGui)
+ {
+ CrashReporter.SetException(ex);
+ }
}
+ TerminateService.Terminate(ex is { } ? 1 : 0);
+ }
+
+ /// <summary>
+ /// Do not call this method it should only be called by TerminateService.
+ /// </summary>
+ private static async Task TerminateApplicationAsync()
+ {
var mainViewModel = MainWindowViewModel.Instance;
if (mainViewModel is { })
{

MODIFY
WalletWasabi/Services/Terminate/TerminateService.cs
WalletWasabi/Services/Terminate/TerminateService.cs
@@ -10,11 +10,11 @@ namespace WalletWasabi.Services.Terminate
public class TerminateService : IDisposable
{
private bool _disposedValue;
- private Func<Exception?, Task> _terminateApplication;
+ private Func<Task> _terminateApplicationAsync;
- public TerminateService(Func<Exception?, Task> terminateApplication)
+ public TerminateService(Func<Task> terminateApplicationAsync)
{
- _terminateApplication = terminateApplication;
+ _terminateApplicationAsync = terminateApplicationAsync;
AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
Console.CancelKeyPress += Console_CancelKeyPress;
}
@@ -44,8 +44,7 @@ namespace WalletWasabi.Services.Terminate
/// <summary>
/// This will terminate the application. This is a blocking method and no return after this call as it will exit the application.
/// </summary>
- /// <param name="criticalException"></param>
- public void Terminate(Exception? criticalException = null)
+ public void Terminate(int exitCode = 0)
{
var prevValue = Interlocked.CompareExchange(ref _terminateStatus, TerminateStatusInProgress, TerminateStatusIdle);
if (prevValue != TerminateStatusIdle)
@@ -61,14 +60,15 @@ namespace WalletWasabi.Services.Terminate
Logger.LogDebug("Terminate application was started.");
// Async termination has to be started on another thread otherwise there is a possibility of deadlock.
- // We still need to block the caller so
+ // We still need to block the caller so ManualResetEvent applied.
using ManualResetEvent resetEvent = new ManualResetEvent(false);
- Task.Run(async () => await _terminateApplication.Invoke(criticalException).ContinueWith((ex) => resetEvent.Set()));
+ Task.Run(async () => await _terminateApplicationAsync.Invoke().ContinueWith((ex) => resetEvent.Set()));
resetEvent.WaitOne();
+ // Indicate that the termination procedure finished. So other callers can return.
Interlocked.Exchange(ref _terminateStatus, TerminateFinished);
- Environment.Exit(criticalException is { } ? 1 : 0);
+ Environment.Exit(exitCode);
}
protected virtual void Dispose(bool disposing)

Simplify and better comments