private static int RunApp() { if (CliArgs.ForceDebugMode) { App.ForceDebugMode = true; } if (CliArgs.IsProxyStart) { StartWithSpotify.HandleProxiedStart(); } var app = new App(); app.InitializeComponent(); app.ShutdownMode = ShutdownMode.OnMainWindowClose; app.DispatcherUnhandledException += (s, e) => OnUnhandledException(e.Exception); TaskScheduler.UnobservedTaskException += (s, e) => { Logger.LogException("Unobserved task exception: ", e.Exception); }; var exitCode = app.Run(); if (CliArgs.IsProxyStart) { StartWithSpotify.HandleProxiedExit(); } return(exitCode); }
public static void InstallUpdateAndRestart(DownloadedUpdate update) { try { Logger.AutoUpdate.LogDebug("Begin install"); File.Delete(TempNewAppPath); using (var tempNewAppFile = File.OpenWrite(TempNewAppPath)) update.UpdateBytes.WriteTo(tempNewAppFile); Logger.AutoUpdate.LogDebug("Extracted update"); File.Delete(TempOldAppPath); File.Move(App.Location, TempOldAppPath); File.Move(TempNewAppPath, App.Location); Logger.AutoUpdate.LogDebug("Replaced executable"); // disable settings with external state before upgrade, as the new version maybe uses a different system. StartWithSpotify.Disable(); Autostart.Disable(); Logger.AutoUpdate.LogDebug("Restarting"); Process.Start(App.Location, "/updateRestart").Dispose(); Application.Current.Dispatcher.Invoke(() => Application.Current.Shutdown()); } catch (Exception e) { Logger.AutoUpdate.LogException("Installation failed:", e); Logger.AutoUpdate.LogInfo("Starting failure cleanup"); // cleanup failed installation // try to restore app executable if (!File.Exists(App.Location)) { if (File.Exists(TempOldAppPath)) { File.Move(TempOldAppPath, App.Location); } else if (File.Exists(TempNewAppPath)) { File.Move(TempNewAppPath, App.Location); } } // delete update file if it still exists File.Delete(TempNewAppPath); Logger.AutoUpdate.LogInfo("Finished failure cleanup"); // rethrow exception throw; } finally { update.Dispose(); } }
private static async Task RunPipeServer(CancellationToken cancellationToken) { using var server = new NamedPipeServerStream(PipeName, PipeDirection.In, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Byte, PipeOptions.Asynchronous); cancellationToken.Register(() => { if (server.IsConnected) { server.Disconnect(); } server.Dispose(); }); await server.WaitForConnectionAsync(cancellationToken); // we received a connection, which means another instance was started -> we bring the window to the front _ = Application.Current.Dispatcher.BeginInvoke(() => { var mainWindow = (MainWindow)Application.Current.MainWindow; mainWindow.Dispatcher.BeginInvoke(() => { mainWindow.Deminimize(); }); }); using var reader = new StreamReader(server); if (await reader.ReadLineAsync() is string line) { if (line == CliArgs.ProxyStartOption) { StartWithSpotify.HandleProxiedStart(); if (!CliArgs.IsProxyStart) { CliArgs = CliArgs with { IsProxyStart = true } } ; } } server.Disconnect(); // restart server await RunPipeServer(cancellationToken); }