Skip to content

Commit

Permalink
Changed RunInBackground() to take an Action delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
integralfx authored May 8, 2019
1 parent 0052300 commit 1e78567
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 85 deletions.
56 changes: 28 additions & 28 deletions MemTestHelper/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

117 changes: 61 additions & 56 deletions MemTestHelper/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ private void Form1_Load(object sender, EventArgs e)

UpdateFormHeight();
UpdateLstCoverageItems();
cboRows.Items.Clear();
InitCboRows();
CentreXYOffsets();
}
Expand All @@ -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++)
{
Expand All @@ -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.
Expand All @@ -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;
}

Expand Down Expand Up @@ -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();

Expand All @@ -232,7 +231,7 @@ private void btnRun_Click(object sender, EventArgs e)
timer.Start();

Activate();
}));
});
}

private void btnStop_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -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++)
{
Expand All @@ -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++)
{
Expand All @@ -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)
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -465,6 +505,7 @@ private bool LoadConfig()
}
catch(FileNotFoundException)
{
Console.WriteLine(CFG_FILENAME + " not found");
return false;
}

Expand Down Expand Up @@ -501,6 +542,7 @@ private bool SaveConfig()
}
catch (Exception)
{
Console.WriteLine("Failed to save " + CFG_FILENAME);
return false;
}
finally
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
});
Expand Down
1 change: 0 additions & 1 deletion MemTestHelper/MemTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ public bool ClickNagMessageBox(string messageBoxCaption, MsgBoxButton button = M
break;
}

// click Ok
WinAPI.ControlClick(hwnd, strBtn);
return true;
}
Expand Down

0 comments on commit 1e78567

Please sign in to comment.