diff --git a/Netch/Forms/MainForm.cs b/Netch/Forms/MainForm.cs index 740445cd..d7ed7922 100644 --- a/Netch/Forms/MainForm.cs +++ b/Netch/Forms/MainForm.cs @@ -611,9 +611,7 @@ namespace Netch.Forms if (!IsWaiting()) { // 停止 - State = State.Stopping; - await MainController.StopAsync(); - State = State.Stopped; + await StopAsyncCore(); return; } @@ -1155,6 +1153,27 @@ namespace Netch.Forms } } + private async Task StopAsyncCore() + { + State = State.Stopping; + await MainController.StopAsync(); + State = State.Stopped; + } + + public void Stop() + { + if (IsWaiting()) + return; + + if (InvokeRequired) + { + Invoke(new Action(Stop)); + return; + } + + StopAsyncCore().Wait(); + } + private bool IsWaiting() { return State == State.Waiting || State == State.Stopped; @@ -1364,8 +1383,7 @@ namespace Netch.Forms if (File.Exists(file)) File.Delete(file); - if (!IsWaiting()) - await MainController.StopAsync(); + Stop(); Dispose(); Environment.Exit(Environment.ExitCode); @@ -1498,7 +1516,7 @@ namespace Netch.Forms public void NotifyTip(string text, int timeout = 0, bool info = true) { - // 会阻塞线程 timeout 秒 + // 会阻塞线程 timeout 秒(?) NotifyIcon.ShowBalloonTip(timeout, UpdateChecker.Name, text, info ? ToolTipIcon.Info : ToolTipIcon.Error); } diff --git a/Netch/Updater/Updater.cs b/Netch/Updater/Updater.cs index 764abd92..04ca286d 100644 --- a/Netch/Updater/Updater.cs +++ b/Netch/Updater/Updater.cs @@ -6,8 +6,11 @@ using System.IO; using System.Linq; using System.Net; using System.Text; +using System.Windows.Threading; using Netch.Controllers; +using Netch.Forms; using Netch.Properties; +using Netch.Services; using Netch.Utils; namespace Netch.Updater @@ -95,10 +98,19 @@ namespace Netch.Updater private void ApplyUpdate() { - // Pre Update + var dispatcher = Dispatcher.CurrentDispatcher; + var mainForm = Global.MainForm; + + #region PreUpdate + + ModeHelper.SuspendWatcher = true; + // Stop and Save + mainForm.Stop(); + Configuration.Save(); + + // Backup Configuration file try { - // Backup Configuration file File.Copy(Configuration.SettingFileFullName, Configuration.SettingFileFullName + ".bak", true); } catch (Exception) @@ -106,6 +118,8 @@ namespace Netch.Updater // ignored } + #endregion + // extract Update file to {tempDirectory}\extract var extractPath = Path.Combine(_tempDirectory, "extract"); int exitCode; @@ -118,11 +132,10 @@ namespace Netch.Updater // move {tempDirectory}\extract\Netch to install folder MoveAllFilesOver(Path.Combine(extractPath, "Netch"), _installDirectory); - // save, release mutex, then exit - Configuration.Save(); - Global.MainForm.Invoke(new Action(() => { Netch.SingleInstance.Dispose(); })); + // release mutex, exit + dispatcher.Invoke(Netch.SingleInstance.Dispose); Process.Start(Global.NetchExecutable); - Global.MainForm.Exit(true, false); + Environment.Exit(0); } private void MarkFilesOld() diff --git a/Netch/Utils/ModeHelper.cs b/Netch/Utils/ModeHelper.cs index b710c055..11c47ff1 100644 --- a/Netch/Utils/ModeHelper.cs +++ b/Netch/Utils/ModeHelper.cs @@ -16,6 +16,8 @@ namespace Netch.Utils private static readonly FileSystemWatcher FileSystemWatcher; + public static bool SuspendWatcher { get; set; } = false; + static ModeHelper() { FileSystemWatcher = new FileSystemWatcher(ModeDirectoryFullName) @@ -33,6 +35,9 @@ namespace Netch.Utils private static void OnModeChanged(object sender, FileSystemEventArgs e) { + if (SuspendWatcher) + return; + Load(); Global.MainForm.LoadModes(); }