private void App_OnStartup(object sender, StartupEventArgs e) { Directory.SetCurrentDirectory(Path.GetDirectoryName(Utils.Utils.GetExecutablePath())); if (e.Args.Contains(Constants.ParameterSetAutoRun)) { if (!AutoStartup.Switch()) { Environment.Exit(1); } Current.Shutdown(); return; } var identifier = $@"Global\{UpdateChecker.Name}_{Directory.GetCurrentDirectory().GetDeterministicHashCode()}"; var singleInstance = new SingleInstance(identifier); if (!singleInstance.IsFirstInstance) { singleInstance.PassArgumentsToFirstInstance(e.Args.Append(Constants.ParameterShow)); Current.Shutdown(); return; } singleInstance.ArgumentsReceived += SingleInstance_ArgumentsReceived; singleInstance.ListenForArgumentsFromSuccessiveInstances(); GlobalConfig.Load(); Current.DispatcherUnhandledException += (o, args) => { if (Interlocked.Increment(ref _exited) == 1) { MessageBox.Show($@"未捕获异常:{args.Exception}", UpdateChecker.Name, MessageBoxButton.OK, MessageBoxImage.Error); Log.ForceLog(args.Exception.ToString()); GlobalConfig.Save(); singleInstance.Dispose(); Current.Shutdown(); } }; Current.Exit += (o, args) => { Utils.Utils.KillFFmpeg(); singleInstance.Dispose(); GlobalConfig.Save(); }; CheckUpdateAsync(); MainWindow = new MainWindow(); if (!e.Args.Contains(Constants.ParameterSilent)) { MainWindow.Show(); } }
private void Application_Exit(object sender, ExitEventArgs e) { if (SingleInstance.IsFirstInstance) //First (unique) instance { SingleInstance.Dispose(); arr.Dispose(); } }
public void TearDown() { if (si != null) { si.Dispose(); si = null; } }
protected override void Dispose(bool disposing) { if (disposing) { singleInstance?.Dispose(); } base.Dispose(disposing); }
public void Wait_for_shutdown() { var single1 = new SingleInstance("test"); single1.TryStartAndWait(); single1.SignalShutdown(); var single2 = new SingleInstance("test"); var task = new Task <bool>(single2.TryStartAndWait); task.Start(); Assert.IsTrue(single2.WaitShutdown.Wait(1.Second())); single1.Dispose(); Assert.IsTrue(task.Wait(1.Second())); Assert.IsTrue(task.Result); }
public void Terminate() => ThreadPool.QueueUserWorkItem(async state => { addOnManager.AddOnAutomaticallyUpdated -= AddOnManagerAddOnAutomaticallyUpdatedHandler; addOnManager.AddOnUpdateAvailable -= AddOnManagerAddOnUpdateAvailableHandler; addOnManager?.Dispose(); worldOfWarcraftInstallation?.Dispose(); await File.WriteAllTextAsync(stateFile.FullName, JsonConvert.SerializeObject(new AppState { MainWindowHeight = MainWindowHeight, MainWindowLeft = MainWindowLeft, MainWindowTop = MainWindowTop, MainWindowWidth = MainWindowWidth, ShowPrereleaseVersions = ShowPrereleaseVersions, ThemeIsDark = ThemeIsDark, ThemeIsHorde = ThemeIsHorde })).ConfigureAwait(false); singleInstance?.Dispose(); await OnUiThreadAsync(() => Shutdown()).ConfigureAwait(false); });
public void TearDown() { si.Dispose(); si = null; }
protected override void OnExit(ExitEventArgs e) { notifier.Dispose(); singleInstance.Dispose(); }
void update(object sender, DoWorkEventArgs e) { init(); try { #if Public ReleaseStream stream = ReleaseStream.Stable40; #else ReleaseStream stream = ReleaseStream.CuttingEdge; #endif bool hasFramework = File.Exists(@"OpenTK.dll"); List <string> files = new List <string>(Directory.GetFiles(Environment.CurrentDirectory)); bool useCurrentDirectory = File.Exists(@"osu!.cfg") || files.Count == 1; if (!hasFramework) { retry_with_default: if (!useCurrentDirectory) { installPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"osu!"); label.Click += chooseFolder; int seconds = 10; while (seconds > 0) { setText(string.Format(LocalisationManager.GetString(OsuString.Maintenance_InstallationPath) + '\n' + LocalisationManager.GetString(OsuString.Maintenance_InstallationPath2), installPath, seconds), true); Thread.Sleep(1000); seconds--; while (installPath == null) { Thread.Sleep(100); } } label.Click -= chooseFolder; } retry: if (installPath != Environment.CurrentDirectory) { string installExecutable = Path.Combine(installPath, @"osu!.exe"); try { if (!Directory.Exists(installPath)) { Directory.CreateDirectory(installPath); } try { File.Delete(installExecutable); } catch (IOException) { //ignore IOExceptions for now } byte[] executable = File.ReadAllBytes(OsuMain.FullPath); using (FileStream fs = File.Create(installExecutable)) fs.Write(executable, 0, executable.Length); } catch (UnauthorizedAccessException) { if (!OsuMain.IsElevated) { if (OsuMain.Elevate(@"-allowuserwrites " + installPath, true)) { goto retry; } else { //error occurred while elevating. try again using the default path. goto retry_with_default; } } } Process.Start(installExecutable); OsuMain.ExitImmediately(); } } try { if (hasFramework) { stream = ConfigManager.sReleaseStream.Value; } } catch (Exception ex) { OsuMain.Repair(true, ex); } setText(string.Empty); tryAgain: setProgress(); if (hasFramework) //force a complete check. { ConfigManager.ResetHashes(); } CommonUpdater.Check(delegate(UpdateStates s) { }, stream); while (true) { switch (CommonUpdater.State) { case UpdateStates.Checking: setText(CommonUpdater.GetStatusString(), detailedTextImportant); setProgress(); Thread.Sleep(20); continue; case UpdateStates.Updating: setText(CommonUpdater.GetStatusString()); setProgress(CommonUpdater.Percentage); Thread.Sleep(20); continue; case UpdateStates.Completed: case UpdateStates.NeedsRestart: setText(LocalisationManager.GetString(OsuString.CommonUpdater_Updated)); break; case UpdateStates.NoUpdate: setText(LocalisationManager.GetString(OsuString.CommonUpdater_Updated), true); Thread.Sleep(1000); break; case UpdateStates.Error: CommonUpdater.Reset(); setProgress(); setText(LocalisationManager.GetString(OsuString.Maintenance_ErrorOccurred), true); Thread.Sleep(5000); goto tryAgain; default: break; } break; } if (!OsuMain.IsWine && !hasFramework) { //create desktop/start menu shortcuts only once per install ShellLinkHelper.Create(icon_path_desktop, Path.Combine(Environment.CurrentDirectory, @"osu!.exe"), @"osu!"); ShellLinkHelper.Create(icon_path_startmenu, Path.Combine(Environment.CurrentDirectory, @"osu!.exe"), @"osu!"); } instance.Dispose(); DialogResult = DialogResult.OK; } catch (Exception ex) { MessageBox.Show(@"Error occured (please report this!):\n\n" + ex.ToString()); } }
public void Dispose() { _singleInstance.Dispose(); }