I'm trying to run simulataneous do while loops but whichever loop is reached first is run and the other isn't.

Here's my code;

namespace sync
{
    public partial class Form1 : Form
    {
        int s = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            s = 0;

            Thread begin1 = new Thread(start1);
            Thread begin2 = new Thread(start2);

            begin1.IsBackground = true;
            begin2.IsBackground = true;

            begin1.Start();
            begin2.Start();
        }

        private void start1()
    {
        this.BeginInvoke(new MethodInvoker(delegate()
         {
             do
             {
                 textBox1.AppendText("1");
                 Thread.Sleep(1000);
                 Application.DoEvents();
             }
             while (s == 0);

         }));
    }

        private void start2()
    {
        this.BeginInvoke(new MethodInvoker(delegate()
        {
            do
            {
                textBox2.AppendText("2");
                Thread.Sleep(1000);
                Application.DoEvents();
            }
            while (s == 0);
        }));
    }


        private void button2_Click(object sender, System.EventArgs e)
        {
            s = 1;
        }
    }
}

The code is simple; button one starts both threads and button 2 stops them.

The threads have the .isbackground property set high thus they should both run fine.

Any pointers would be greatly appreciated.

Recommended Answers

All 3 Replies

You cannot access to AppendText or any property of TextBox over threads. You will have to use delegates to access to it on controls, not on your do, while loop:

namespace sync
{
    public partial class Form1 : Form
    {
        int s = 0;
        delegate void MyDelegate1(string msg);
        delegate void MyDelegate2(string msg);

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            s = 0;

            Thread begin1 = new Thread(start1);
            Thread begin2 = new Thread(start2);

            begin1.IsBackground = true;
            begin2.IsBackground = true;

            begin1.Start();
            begin2.Start();
        }

    private void start1()
    {        
             do
             {                
                 UpdateTextBox1("1");
                 Thread.Sleep(1000);
                 Application.DoEvents();
             }
             while (s == 0);        
    }

    private void start2()
    {
            do
            {
                UpdateTextBox2("2");
                Thread.Sleep(1000);
                Application.DoEvents();
            }
            while (s == 0);        
    }
 
       private void UpdateTextBox1(string text)
       {
           if(textBox1.InvokeRequired)
               textBox1.invoke(new MyDelegat1(UpdateTextBox1), new object [] {text});
           else
               textBox1.Text = text;
       }

       private void UpdateTextBox2(string text)
       {
           if(textBox2.InvokeRequired)
               textBox2.invoke(new MyDelegat2(UpdateTextBox2), new object [] {text});
           else
               textBox2.Text = text;
       }

        private void button2_Click(object sender, System.EventArgs e)
        {
            s = 1;
        }
    }
}

This should do the trick!

^^^^^^^

Awesome! That did the trick!

Thanks!

cool, I`m glad it did.
btw, you could vote up a bit if it helped :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.