I'm having a real difficult time getting two threads to run simultaneously.....
Both of the threads contain 'do while' loops and depending on which one is reached first, it locks out the other thread.
My code is as follows; both threads are started together by the click of a button.
The first thread is;
private void MC1boot()
{
this.BeginInvoke(new MethodInvoker(delegate()
{
textBox1.AppendText("Testing MC Boot UseIT..." + Environment.NewLine);
string match2 = "I'M ALIVE";
double y = 45000; // Timeout (ms)
MCport1.Open(); // Open COM5
textBox1.AppendText("MC com port opened.." + Environment.NewLine);
textBox1.AppendText("Receiving Data.." + Environment.NewLine);
sw1.Restart();
//*******************************
// BOTH reach here fine
//*******************************
do
{
double x = (((y - sw1.ElapsedMilliseconds) / 1000));
int i = Convert.ToInt32(x);
if (i > 9)
{
dataGridView1.Rows[0].Cells[3].Value = ("00:" + i);
Application.DoEvents();
}
else
{
dataGridView1.Rows[0].Cells[3].Value = ("00:" + i);
Application.DoEvents();
}
textBox2.AppendText(MCport1.ReadExisting()); // Write serial data stream to textbox
Application.DoEvents();
}
while ((sw1.ElapsedMilliseconds < y) && (s == 0)); // 45 sec timeout + 'stop' int
if (Regex.IsMatch(textBox2.Text, match2)) // When 'I'M ALIVE' is received....
{
textBox1.AppendText("PASS" + Environment.NewLine);
}
else
{
textBox1.AppendText("FAIL" + Environment.NewLine);
}
}));
}
The second thread is;
private void LIU1boot()
{
this.BeginInvoke(new MethodInvoker(delegate()
{
textBox3.AppendText("Testing LIU Boot UseIT..." + Environment.NewLine);
string match2 = "I'M ALIVE";
double y = 45000; // Timeout (ms)
LIUport1.Open(); // Open COM6
textBox3.AppendText("LIU com port opened.." + Environment.NewLine);
textBox3.AppendText("Receiving Data.." + Environment.NewLine);
sw2.Restart();
//*******************************
// BOTH reach here fine
//*******************************
do
{
double x = (((y - sw1.ElapsedMilliseconds) / 1000));
int i = Convert.ToInt32(x);
if (i > 9)
{
dataGridView1.Rows[1].Cells[3].Value = ("00:" + i);
Application.DoEvents();
}
else
{
dataGridView1.Rows[1].Cells[3].Value = ("00:" + i);
Application.DoEvents();
}
textBox4.AppendText(LIUport1.ReadExisting()); // Write serial data stream to textbox
Application.DoEvents();
}
while ((sw1.ElapsedMilliseconds < y) && (s == 0)); // 45 sec timeout + 'stop' int
if (Regex.IsMatch(textBox4.Text, match2)) // When 'I'M ALIVE' is received....
{
textBox3.AppendText("PASS" + Environment.NewLine);
}
else
{
textBox3.AppendText("FAIL" + Environment.NewLine);
}
}));
}
They are essentially the same function except they read and write from different places.
Any help would be greatly appreciated.