From 2a165c79dfbc86c30338e3e9b041af00a538e65a Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Sun, 21 Mar 2021 22:44:15 +0800 Subject: [PATCH] Start Profile, Refactor Save LastSelectedServer/Mode --- Netch/Forms/MainForm.Designer.cs | 4 +-- Netch/Forms/MainForm.cs | 26 +++++------------- Netch/Netch.cs | 27 +++++++++++++++++++ .../ServerConverterWithTypeDiscriminator.cs | 1 + 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/Netch/Forms/MainForm.Designer.cs b/Netch/Forms/MainForm.Designer.cs index 162ae14e..9c323268 100644 --- a/Netch/Forms/MainForm.Designer.cs +++ b/Netch/Forms/MainForm.Designer.cs @@ -423,7 +423,7 @@ this.ModeComboBox.Size = new System.Drawing.Size(546, 24); this.ModeComboBox.TabIndex = 2; this.ModeComboBox.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.ComboBox_DrawItem); - this.ModeComboBox.SelectedIndexChanged += new System.EventHandler(this.ModeComboBox_SelectedIndexChanged); + this.ModeComboBox.SelectionChangeCommitted += new System.EventHandler(this.ModeComboBox_SelectionChangeCommitted); // // ServerComboBox // @@ -438,7 +438,7 @@ this.ServerComboBox.Size = new System.Drawing.Size(546, 24); this.ServerComboBox.TabIndex = 1; this.ServerComboBox.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.ComboBox_DrawItem); - this.ServerComboBox.SelectedIndexChanged += new System.EventHandler(this.ServerComboBox_SelectedIndexChanged); + this.ServerComboBox.SelectionChangeCommitted += new System.EventHandler(this.ServerComboBox_SelectionChangeCommitted); // // tableLayoutPanel2 // diff --git a/Netch/Forms/MainForm.cs b/Netch/Forms/MainForm.cs index 515991ad..767305bb 100644 --- a/Netch/Forms/MainForm.cs +++ b/Netch/Forms/MainForm.cs @@ -30,7 +30,6 @@ namespace Netch.Forms private readonly Dictionary _mainFormText = new(); - private bool _comboBoxInitialized; private bool _textRecorded; public MainForm() @@ -85,6 +84,8 @@ namespace Netch.Forms private void MainForm_Load(object sender, EventArgs e) { + Netch.TimePoint("MainForm ctor (Pre MainForm Load)"); + // 计算 ComboBox绘制 目标宽度 RecordSize(); @@ -93,7 +94,6 @@ namespace Netch.Forms ModeHelper.Load(); LoadModes(); - _comboBoxInitialized = true; // 加载翻译 TranslateControls(); @@ -121,6 +121,8 @@ namespace Netch.Forms if (Global.Settings.StartWhenOpened) ControlButton_Click(null, null); }); + + Netch.TimePoint("Post Form Load", false); } private void RecordSize() @@ -735,13 +737,9 @@ namespace Netch.Forms private void LoadServers() { - var comboBoxInitialized = _comboBoxInitialized; - _comboBoxInitialized = false; - ServerComboBox.Items.Clear(); ServerComboBox.Items.AddRange(Global.Settings.Server.ToArray()); SelectLastServer(); - _comboBoxInitialized = comboBoxInitialized; } public void SelectLastServer() @@ -756,11 +754,8 @@ namespace Netch.Forms // 如果当前 ServerComboBox 中没元素,不做处理 } - private void ServerComboBox_SelectedIndexChanged(object sender, EventArgs o) + private void ServerComboBox_SelectionChangeCommitted(object sender, EventArgs o) { - if (!_comboBoxInitialized) - return; - Global.Settings.ServerComboBoxSelectedIndex = ServerComboBox.SelectedIndex; } @@ -859,14 +854,10 @@ namespace Netch.Forms public void LoadModes() { - var comboBoxInitialized = _comboBoxInitialized; - _comboBoxInitialized = false; - ModeComboBox.Items.Clear(); - ModeComboBox.Items.AddRange(Global.Modes.ToArray()); + ModeComboBox.Items.AddRange(Global.Modes.Cast().ToArray()); ModeComboBox.Tag = null; SelectLastMode(); - _comboBoxInitialized = comboBoxInitialized; } public void SelectLastMode() @@ -881,11 +872,8 @@ namespace Netch.Forms // 如果当前 ModeComboBox 中没元素,不做处理 } - private void ModeComboBox_SelectedIndexChanged(object sender, EventArgs o) + private void ModeComboBox_SelectionChangeCommitted(object sender, EventArgs o) { - if (!_comboBoxInitialized) - return; - try { Global.Settings.ModeComboBoxSelectedIndex = Global.Modes.IndexOf((Models.Mode) ModeComboBox.SelectedItem); diff --git a/Netch/Netch.cs b/Netch/Netch.cs index 45d77260..726cbacf 100644 --- a/Netch/Netch.cs +++ b/Netch/Netch.cs @@ -16,6 +16,28 @@ namespace Netch { public static class Netch { + private static readonly Stopwatch Stopwatch = new(); + + public static void StartStopwatch(string name) + { + if (Stopwatch.IsRunning) + throw new Exception(); + + Stopwatch.Start(); + Console.WriteLine($"Start {name} Stopwatch"); + } + + public static void TimePoint(string name, bool restart = true) + { + if (!Stopwatch.IsRunning) + throw new Exception(); + + Stopwatch.Stop(); + Console.WriteLine($"{name} Stopwatch: {Stopwatch.ElapsedMilliseconds}"); + if (restart) + Stopwatch.Restart(); + } + /// /// 应用程序的主入口点 /// @@ -28,6 +50,7 @@ namespace Netch if (args.Contains("-console")) AttachConsole(); #endif + StartStopwatch("Netch"); // 设置当前目录 Directory.SetCurrentDirectory(Global.NetchDir); @@ -43,9 +66,11 @@ namespace Netch if (!Directory.Exists(item)) Directory.CreateDirectory(item); + TimePoint("Clean Old, Create Directory"); // 加载配置 Configuration.Load(); + TimePoint("Load Configuration"); // 检查是否已经运行 if (!Global.Mutex.WaitOne(0, false)) { @@ -79,6 +104,8 @@ namespace Netch Logging.Info($"版本: {UpdateChecker.Owner}/{UpdateChecker.Repo}@{UpdateChecker.Version}"); Task.Run(() => { Logging.Info($"主程序 SHA256: {Utils.Utils.SHA256CheckSum(Global.NetchExecutable)}"); }); + TimePoint("Get Info, Pre-Form"); + // 绑定错误捕获 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); Application.ThreadException += Application_OnException; diff --git a/Netch/Utils/ServerConverterWithTypeDiscriminator.cs b/Netch/Utils/ServerConverterWithTypeDiscriminator.cs index 97d119b8..aa73e16a 100644 --- a/Netch/Utils/ServerConverterWithTypeDiscriminator.cs +++ b/Netch/Utils/ServerConverterWithTypeDiscriminator.cs @@ -20,6 +20,7 @@ namespace Netch.Utils } catch { + // Unsupported Server Type return JsonSerializer.Deserialize(jsonElement.GetRawText(), new JsonSerializerOptions())!; } }