private static async Task <ErrorCode> StartAppAsync(string[] args, RunOptions options) { SetDirectories(options); try { using (SyncContext = GlobalContext.Create()) { if (SyncContext.IsFirst) { // First instance, start like normal InitLogger(options); await InitProgramAsync(options); SyncContext.BeginListen(); try { SkipUpdateCheck = options.NoAutoUpdate; var code = (ErrorCode)BuildAvaloniaApp().StartWithClassicDesktopLifetime(args, ShutdownMode.OnMainWindowClose); if (code != ErrorCode.NoError) { Log.Warning("Application returned error code '{0}'", code); } else { Log.Debug("Application exited gracefully"); } return(code); } catch (Exception ex) { Log.Error(ex, "Application crashed"); return(ErrorCode.General); } finally { SyncContext.EndListen(); UnloadProgram(); } } else { // App already running, pass arguments and exit try { var message = Parser.Default.FormatCommandLine(options); await SyncContext.SendMessageAsync(message); return(ErrorCode.NoError); } catch { return(ErrorCode.General); } } } } finally { SyncContext = null; } }