I'm having issues with my background worker not completing and subsequently hanging after one run. The background worker handles a program running a linear stage and so it is important that the timing is right (hence all of the Thread.Sleep()
calls.)
here is my code:
public MainForm()
{
InitializeComponent();
bgW2.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgW2_RunWorkerCompleted);
}
private void bgW2_DoWork(object sender, DoWorkEventArgs e)
{
string program = @"C:\Users\mikegjohn\source\repos\CLL AutoForce\Test.csv";
var stream = new FileStream(program, FileMode.Open, FileAccess.Read);
using (var streamReader = new StreamReader(stream))
{
string ln;
while ((ln = streamReader.ReadLine()) != null)
{
string[] lnArr = ln.Split(',');
switch (lnArr[1])
{
case "0":
sendCommand("L-");
string status = "";
while (status != "0")
{
Thread.Sleep(250);
status = loopCommands("PS");
}
Thread.Sleep(1000);
break;
case "1":
string p = Fn.convertPos(lnArr[3]);
sendCommand("X"+p);
string s = Convert.ToString(Fn.backconvert(loopCommands("PX")));
while (s != lnArr[3])
{
Thread.Sleep(250);
s = Convert.ToString(Fn.backconvert(loopCommands("PX")));
}
Thread.Sleep(1000);
break;
case "2":
Fn.MeasureLabel();
break;
case "3":
sendCommand("HSPD=500000");
Thread.Sleep(500);
sendCommand("LSPD=250000");
Thread.Sleep(500);
sendCommand("X0");
string x = Convert.ToString(Fn.backconvert(loopCommands("PX")));
while (x != lnArr[4])
{
Thread.Sleep(250);
s = Convert.ToString(Fn.backconvert(loopCommands("PX")));
}
Thread.Sleep(1000);
sendCommand("HSPD=250000");
sendCommand("LSPD=50000");
break;
}
}
}
}
private void btnRun_Click(object sender, EventArgs e)
{
bgW2.RunWorkerAsync();
}
private void bgW2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("Program completed");
}
The Messagebox.Show
never fires. I have taken a look at some other similar issues with this which points mainly to blocking of the UI thread. Can anyone point me in the right direction with this?