I have a form application in C# which does what it needs to however all on the UI thread. Its is not computationally expensive but fairly time consuming as it waits for responses from other systems and locks up the UI during this.
Below is a snippet from my routine.
private void btnRun_Click(object sender, EventArgs e)
{
TextAppend("Checking Laser Marker TCP/IP...");
foreach(string t in Steps)
{
var itm = new ListViewItem(t);
listView1.Items.Add(itm);
}
listView1.Items[0].BackColor = Color.PaleGoldenrod;
progressBar1.Value = 0;
LabelShows(1);
//Check the TCP/IP connection
progressBar1.Value = 21;
bool checkIP = RunModule.CheckConnection("GI SN", IP, port, this);
if (checkIP == false)
{
listView1.Items[0].BackColor = Color.Red;
MessageOK("Failed to connect to the Laser Marker! Please check IP, Port and serial numbers match the Laser marker.", "warn");
LabelShows(0);
return;
}
}
The section:
bool checkIP = RunModule.CheckConnection("GI SN", IP, port, this);
if (checkIP == false)
{
listView1.Items[0].BackColor = Color.Red;
MessageOK("Failed to connect to the Laser Marker! Please check IP, Port and serial numbers match the Laser marker.", "warn");
LabelShows(0);
return;
}
Occurs in a similar fashion several time further down the routine with different functions. All functions run from a different class.
I have looked at using backgroundworkers but these report progress rather than a bool. I have looked at Task.Run()
but I cannot see a way to implement it. Can anyone provide advice on how to better structure my routine?