cannot display thread result on a textbox - part2

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • warjie
    New Member
    • Dec 2010
    • 2

    cannot display thread result on a textbox - part2

    Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on.

    what's wrong with this? i want to display the thread result to a text box. here's my code: thanks

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    
    namespace MultiThread
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            
            private void Form1_Load(object sender, EventArgs e)
            {
                
            }
            private void Do()
            {
                for (int i = 0; i <= 100; i++)
                {
                    textBox1.Text = textBox1.Text + "hello";
                    Thread.Sleep(100);
                }
            }
            private void button1_Click(object sender, EventArgs e)
            {
                //// Creates a new thread
                ThreadStart tr = new ThreadStart(Do);
                Thread t = new Thread(tr);
                t.Start();
     
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
            }
    
            public void TestThread()
            {
                for (int i = 0; i <= 1000; i++)
                {
                    textBox1.Text = textBox1.Text + "Thread";
                    Thread.Sleep(100);
                }
            }
        }
    }
  • Joseph Martell
    Recognized Expert New Member
    • Jan 2010
    • 198

    #2
    The error is pretty descriptive. You have created a thread and in that thread operation you are trying to modify an object that was created in another thread. This means that there are 2 threads that can access and modify a the same object.

    This is the definition of not thread safe.

    Check out this article on MSDN. BeginInvoke is the answer to your problem.

    Comment

    • Aimee Bailey
      Recognized Expert New Member
      • Apr 2010
      • 197

      #3
      Even though there is documentation out there, i believe it's most educational to give an example on how to make it thread safe. The code you written is almost there, but you must remember that when you want to interact with the form again, you need to get back into the form's thread. To do this you can use a worker sub that moves itself back into the form's thread like so:

      Code:
       private void Do()
       {
           for (int i = 0; i <= 100; i++)
           {
               DoTwo("hello");
               Thread.Sleep(100);
           }
       }
       
       private delegate void DoTwoHandler(string text);
       private void DoTwo(string text)
       {
           if (InvokeRequired) 
           {
               BeginInvoke(new DoTwoHandler(DoTwo), text); 
               return;
           }
           textBox1.Text = textBox1.Text + text;
       }
       
       private void button1_Click(object sender, EventArgs e)
       {
           ThreadStart ts = new ThreadStart(Do);
           Thread thread = new Thread(ts);
           thread.Start();
       }
      This way Do() stays in it's own thread, and when it need's to interact with the form, DoTwo() is called. DoTwo() checks to see which thread it is working within, and when it find's its not in the same thread as the form, it invokes itself through a delegate to get back into the same thread as the form.

      Doing this implies the possibility of a little overhead calling DoTwo() twice, however as the overhead occurs within the thread you created, not the form's thread, you shouldnt notice a peformance hit.

      Good luck, and make sure to practice this if you wish to work with multiple threads :)

      Aimee.

      Comment

      Working...