From 1e78567fc78c578c597d8f15f7498108d7cc50c1 Mon Sep 17 00:00:00 2001 From: integralfx Date: Wed, 8 May 2019 12:05:28 +1000 Subject: [PATCH] Changed RunInBackground() to take an Action delegate --- MemTestHelper/Form1.Designer.cs | 56 +++++++-------- MemTestHelper/Form1.cs | 117 +++++++++++++++++--------------- MemTestHelper/MemTest.cs | 1 - 3 files changed, 89 insertions(+), 85 deletions(-) diff --git a/MemTestHelper/Form1.Designer.cs b/MemTestHelper/Form1.Designer.cs index f0b8bb7..bef46cd 100644 --- a/MemTestHelper/Form1.Designer.cs +++ b/MemTestHelper/Form1.Designer.cs @@ -29,7 +29,7 @@ protected override void Dispose(bool disposing) private void InitializeComponent() { this.tabControl = new System.Windows.Forms.TabControl(); - this.tab_main = new System.Windows.Forms.TabPage(); + this.tabMain = new System.Windows.Forms.TabPage(); this.btn_hide = new System.Windows.Forms.Button(); this.lstCoverage = new System.Windows.Forms.ListView(); this.hdr_no = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); @@ -72,7 +72,7 @@ private void InitializeComponent() this.lbl_discord = new System.Windows.Forms.Label(); this.lbl_version = new System.Windows.Forms.Label(); this.tabControl.SuspendLayout(); - this.tab_main.SuspendLayout(); + this.tabMain.SuspendLayout(); this.tab_settings.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.udWinHeight)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.udYOffset)).BeginInit(); @@ -85,7 +85,7 @@ private void InitializeComponent() // tabControl // this.tabControl.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom))); - this.tabControl.Controls.Add(this.tab_main); + this.tabControl.Controls.Add(this.tabMain); this.tabControl.Controls.Add(this.tab_settings); this.tabControl.Controls.Add(this.tab_about); this.tabControl.Location = new System.Drawing.Point(0, 0); @@ -97,28 +97,28 @@ private void InitializeComponent() // // tab_main // - this.tab_main.Controls.Add(this.btn_hide); - this.tab_main.Controls.Add(this.lstCoverage); - this.tab_main.Controls.Add(this.lblSpeedValue); - this.tab_main.Controls.Add(this.lbl_speed); - this.tab_main.Controls.Add(this.lblEstimatedTime); - this.tab_main.Controls.Add(this.lbl_estimated); - this.tab_main.Controls.Add(this.lblElapsedTime); - this.tab_main.Controls.Add(this.lbl_elapsed); - this.tab_main.Controls.Add(this.btn_show); - this.tab_main.Controls.Add(this.btnStop); - this.tab_main.Controls.Add(this.btnRun); - this.tab_main.Controls.Add(this.cboThreads); - this.tab_main.Controls.Add(this.lbl_num_threads); - this.tab_main.Controls.Add(this.txtRAM); - this.tab_main.Controls.Add(this.btnAutoRAM); - this.tab_main.Location = new System.Drawing.Point(4, 22); - this.tab_main.Margin = new System.Windows.Forms.Padding(0); - this.tab_main.Name = "tab_main"; - this.tab_main.Size = new System.Drawing.Size(219, 356); - this.tab_main.TabIndex = 0; - this.tab_main.Text = "Main"; - this.tab_main.UseVisualStyleBackColor = true; + this.tabMain.Controls.Add(this.btn_hide); + this.tabMain.Controls.Add(this.lstCoverage); + this.tabMain.Controls.Add(this.lblSpeedValue); + this.tabMain.Controls.Add(this.lbl_speed); + this.tabMain.Controls.Add(this.lblEstimatedTime); + this.tabMain.Controls.Add(this.lbl_estimated); + this.tabMain.Controls.Add(this.lblElapsedTime); + this.tabMain.Controls.Add(this.lbl_elapsed); + this.tabMain.Controls.Add(this.btn_show); + this.tabMain.Controls.Add(this.btnStop); + this.tabMain.Controls.Add(this.btnRun); + this.tabMain.Controls.Add(this.cboThreads); + this.tabMain.Controls.Add(this.lbl_num_threads); + this.tabMain.Controls.Add(this.txtRAM); + this.tabMain.Controls.Add(this.btnAutoRAM); + this.tabMain.Location = new System.Drawing.Point(4, 22); + this.tabMain.Margin = new System.Windows.Forms.Padding(0); + this.tabMain.Name = "tab_main"; + this.tabMain.Size = new System.Drawing.Size(219, 356); + this.tabMain.TabIndex = 0; + this.tabMain.Text = "Main"; + this.tabMain.UseVisualStyleBackColor = true; // // btn_hide // @@ -593,8 +593,8 @@ private void InitializeComponent() this.Load += new System.EventHandler(this.Form1_Load); this.Resize += new System.EventHandler(this.Form1_Resize); this.tabControl.ResumeLayout(false); - this.tab_main.ResumeLayout(false); - this.tab_main.PerformLayout(); + this.tabMain.ResumeLayout(false); + this.tabMain.PerformLayout(); this.tab_settings.ResumeLayout(false); this.tab_settings.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.udWinHeight)).EndInit(); @@ -611,7 +611,7 @@ private void InitializeComponent() #endregion private System.Windows.Forms.TabControl tabControl; - private System.Windows.Forms.TabPage tab_main; + private System.Windows.Forms.TabPage tabMain; private System.Windows.Forms.TabPage tab_settings; private System.Windows.Forms.TabPage tab_about; private System.Windows.Forms.Button btnAutoRAM; diff --git a/MemTestHelper/Form1.cs b/MemTestHelper/Form1.cs index 78db86a..22796be 100644 --- a/MemTestHelper/Form1.cs +++ b/MemTestHelper/Form1.cs @@ -127,7 +127,6 @@ private void Form1_Load(object sender, EventArgs e) UpdateFormHeight(); UpdateLstCoverageItems(); - cboRows.Items.Clear(); InitCboRows(); CentreXYOffsets(); } @@ -145,7 +144,7 @@ private void Form1_Resize(object sender, EventArgs e) { // minimise MemTest instances case FormWindowState.Minimized: - RunInBackground(new MethodInvoker(delegate + RunInBackground(() => { for (var i = 0; i < threads; i++) { @@ -155,12 +154,12 @@ private void Form1_Resize(object sender, EventArgs e) Thread.Sleep(10); } } - })); + }); break; // restore previous state of MemTest instances case FormWindowState.Normal: - RunInBackground(new MethodInvoker(delegate + RunInBackground(() => { /* * isMinimised is true when user clicked the hide button. @@ -177,15 +176,15 @@ private void Form1_Resize(object sender, EventArgs e) } } - // user may have changed offsets while minimised + // User may have changed offsets while minimised. LayOutMemTests(); - // hack to bring form to top + // Hack to bring form to top. TopMost = true; Thread.Sleep(10); TopMost = false; } - })); + }); break; } @@ -221,7 +220,7 @@ private void btnRun_Click(object sender, EventArgs e) chkStartMin.Enabled = false; // Run in background as StartMemTests() can block. - RunInBackground(new MethodInvoker(delegate + RunInBackground(() => { StartMemTests(); @@ -232,7 +231,7 @@ private void btnRun_Click(object sender, EventArgs e) timer.Start(); Activate(); - })); + }); } private void btnStop_Click(object sender, EventArgs e) @@ -271,7 +270,7 @@ private void btnShow_Click(object sender, EventArgs e) { // Run in background as Thread.Sleep can lockup the GUI. var threads = (int)cboThreads.SelectedItem; - RunInBackground(new MethodInvoker(delegate + RunInBackground(() => { for (var i = 0; i < threads; i++) { @@ -290,13 +289,13 @@ private void btnShow_Click(object sender, EventArgs e) LayOutMemTests(); Activate(); - })); + }); } private void btnHide_Click(object sender, EventArgs e) { var threads = (int)cboThreads.SelectedItem; - RunInBackground(new MethodInvoker(delegate + RunInBackground(() => { for (var i = 0; i < threads; i++) { @@ -309,12 +308,12 @@ private void btnHide_Click(object sender, EventArgs e) } isMinimised = true; - })); + }); } private void offset_Changed(object sender, EventArgs e) { - RunInBackground(new MethodInvoker(delegate { LayOutMemTests(); })); + RunInBackground(() => { LayOutMemTests(); }); } private void btnCenter_Click(object sender, EventArgs e) @@ -357,6 +356,47 @@ private void udWinHeight_ValueChanged(object sender, EventArgs e) // Helper Functions // + private void InitCboThreads() + { + cboThreads.Items.Clear(); + + for (var i = 0; i < MAX_THREADS; i++) + cboThreads.Items.Add(i + 1); + + cboThreads.SelectedItem = NUM_THREADS; + } + + private void InitCboRows() + { + cboRows.Items.Clear(); + + var threads = (int)cboThreads.SelectedItem; + + for (var i = 1; i <= threads; i++) + { + if (threads % i == 0) + cboRows.Items.Add(i); + } + + cboRows.SelectedItem = threads % 2 == 0 ? 2 : 1; + } + + private void InitLstCoverage() + { + // Stop flickering: https://stackoverflow.com/a/15268338 + var method = typeof(ListView).GetMethod("SetStyle", BindingFlags.Instance | BindingFlags.NonPublic); + method.Invoke(lstCoverage, new object[] { ControlStyles.OptimizedDoubleBuffer, true }); + + for (var i = 0; i <= (int)cboThreads.SelectedItem; i++) + { + string[] row = { i.ToString(), "-", "-" }; + // first row is total + if (i == 0) row[0] = "T"; + + lstCoverage.Items.Add(new ListViewItem(row)); + } + } + // Returns free RAM in MB. private ulong GetFreeRAM() { @@ -465,6 +505,7 @@ private bool LoadConfig() } catch(FileNotFoundException) { + Console.WriteLine(CFG_FILENAME + " not found"); return false; } @@ -501,6 +542,7 @@ private bool SaveConfig() } catch (Exception) { + Console.WriteLine("Failed to save " + CFG_FILENAME); return false; } finally @@ -607,22 +649,6 @@ private bool ValidateInput() return true; } - private void InitLstCoverage() - { - // Stop flickering: https://stackoverflow.com/a/15268338 - var method = typeof(ListView).GetMethod("SetStyle", BindingFlags.Instance | BindingFlags.NonPublic); - method.Invoke(lstCoverage, new object[] { ControlStyles.OptimizedDoubleBuffer, true }); - - for (var i = 0; i <= (int)cboThreads.SelectedItem; i++) - { - string[] row = { i.ToString(), "-", "-" }; - // first row is total - if (i == 0) row[0] = "T"; - - lstCoverage.Items.Add(new ListViewItem(row)); - } - } - private void UpdateLstCoverageItems() { var threads = (int)cboThreads.SelectedItem; @@ -642,27 +668,6 @@ private void UpdateLstCoverageItems() } } - private void InitCboThreads() - { - for (var i = 0; i < MAX_THREADS; i++) - cboThreads.Items.Add(i + 1); - - cboThreads.SelectedItem = NUM_THREADS; - } - - private void InitCboRows() - { - var threads = (int)cboThreads.SelectedItem; - - for (var i = 1; i <= threads; i++) - { - if (threads % i == 0) - cboRows.Items.Add(i); - } - - cboRows.SelectedItem = threads % 2 == 0 ? 2 : 1; - } - private void CentreXYOffsets() { var screen = Screen.FromControl(this).Bounds; @@ -837,8 +842,8 @@ private bool IsAnyMemTestStopping() private void ClickBtnStop() { var currTab = tabControl.SelectedTab; - if (currTab != tab_main) - tabControl.SelectedTab = tab_main; + if (currTab != tabMain) + tabControl.SelectedTab = tabMain; btnStop.PerformClick(); tabControl.SelectedTab = currTab; @@ -856,7 +861,7 @@ private void ShowErrorMsgBox(string msg) private bool IsAllFinished() { - for (int i = 0; i < (int)cboThreads.SelectedItem; i++) + for (var i = 0; i < (int)cboThreads.SelectedItem; i++) { if (!memtests[i].Finished) return false; @@ -865,10 +870,10 @@ private bool IsAllFinished() return true; } - private void RunInBackground(Delegate method) + private void RunInBackground(Action method) { BackgroundWorker bw = new BackgroundWorker(); - bw.DoWork += new DoWorkEventHandler(delegate (object s, DoWorkEventArgs args) + bw.DoWork += new DoWorkEventHandler((sender, e) => { Invoke(method); }); diff --git a/MemTestHelper/MemTest.cs b/MemTestHelper/MemTest.cs index d6e6f2a..9a3f24b 100644 --- a/MemTestHelper/MemTest.cs +++ b/MemTestHelper/MemTest.cs @@ -195,7 +195,6 @@ public bool ClickNagMessageBox(string messageBoxCaption, MsgBoxButton button = M break; } - // click Ok WinAPI.ControlClick(hwnd, strBtn); return true; }