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.

Edited 5 Years Ago by buster2209: n/a

why dont you use Thread.IsAlive Property with your do while loop condition..?

somthing like

//in thread 1 
while ((sw1.ElapsedMilliseconds < y) && (s == 0) && MyThread2.IsAlive())

if the condition fails means the MyThread2 is completed and then you can be out of there. . .

It seems your code is riddled with cross threading violations. sw1 and datagrid1 are both being accessed by each thread simultaneously.

Can you imagine what might happen inside your computer if thread 1 is writing to a row in the datagrid while another thread is trying to write to it as well? Total meltdown, that's what.

You need to explore delegates, and invoking them from within threads. I think theres a few thousand threads on this site about this exact issue.

I don't have time to write the code for you, so I will just upload 2 examples of some things I did for work.

1 is a google pinger that runs in the background pinging google, and keeps track of average and peak latencies.

The other is a .Net driver implementation for a data aquisition device. I'm assuming you don't have the particular device to test it with, so it will be of no use to you if you try to run it. Just look at the delegates/threading code. Note: It was all written in Visual Studio 2010, so the .sln file will not work if you have an earlier version.

Thanks for the examples!

Creating two separate stopwatches (sw1) is not a problem but how do I go about getting multiple threads to access the same datagrid simultaneously?

I want to be able to have multiple threads doing different things but outputting the data in the same datagrid

Edited 5 Years Ago by buster2209: n/a

This is achieved by delegates. Basically a delegate is a function pointer to a function of a prespecified argument list. There's examples of it in the code I posted, but I'll break it down a bit better:

delegate delVoidString(string s);

void main()
{
    thread t = new thread(new paramaterizedthreadstart(threadFunc, new delVoidString(doStuff)));
    t.start();
}

void threadFunc(delVoidString _Callback)
{
    string s;
    do
    {
      if (thread_leaving_condition)
         break;
      s = do_some_work;
      invoke(_Callback,sData);
    } while (true);
}

void doStuff (string s)
{
    Console.Writeline("This message is threadsafe, and is being accessed by a different thread" + Environment.newline + s);
}

Not too sure if that will compile properly. But it will give you a good idea of how it works.

Edited 5 Years Ago by skatamatic: n/a

Thank you for the advice. Even though I have not implemented this solution, I can see that it is my incorrect use of delegates that is causing the error thus I will mark this thread as solved

This question has already been answered. Start a new discussion instead.