locked
How can I made a separate thread dump text into my textbox in my form? RRS feed

  • Question

  • I bought some software and tried to use it with my C# application.  The sample code they gave me was a class that used 'console.writeln', but I decided to have it to write to a textbox in an asp.net application.  

    That is where I ran into this error:

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

    How do I get around this?  My code is below:

    static Connector connector; static int gcount = 0; public Form1() { InitializeComponent(); } private void buttonSubmit_Click(object sender, EventArgs e) { beginning(); } public void beginning() { // Initialize a new Connector and add event handlers connector = new Connector(); connector.DeviceConnected += new EventHandler(OnDeviceConnected); connector.DeviceConnectFail += new EventHandler(OnDeviceFail); connector.DeviceValidating += new EventHandler(OnDeviceValidating);

    /* the above events are supposed to call "AddToBox" -- see below... */ connector.ConnectScan("COM40"); } private void AddToBox(string thestring) { string strout; strout = this.textBoxOut.Text; gcount++; strout = gcount + ") " + thestring + strout; this.textBoxOut.Text = strout; if (gcount > 50) { connector.Close(); } }


    Sunday, September 13, 2015 11:19 AM

Answers

  • Hi malikmeister,

    this.textBoxOut.Text = strout;

    Change your upper code as below.

    this.textBoxOut.Invoke(new Action(() => { this.textBoxOut.Text = strout; }));
    Best Regards,
    Li Wang
    • Marked as answer by malikmeister Tuesday, September 15, 2015 10:41 AM
    Monday, September 14, 2015 6:50 AM
  • Writing any GUI element from a thread other then the main thread can cause a bajillion issues. Wich is why you were protected from doing it by accident with .NET 2.0. The Cross thread exception is your friend, not your enemy!

    You can prepare the data to be written in another thread. You can retrieve it in that thread (from a file, database or network resources). You can process it in that thread. Even conact it to one singular string (avoid writing to GUI elements in a Loop).
    But the actuall assignment should always be done by the GUI thread via Invoke:

    Invoke

    Normally you would have to deal with ownership on the data in addition, but luckily string is inmutable like the basic types. So you can transmit a string between threads without any wierd side effects.

    • Marked as answer by malikmeister Tuesday, September 15, 2015 10:41 AM
    Monday, September 14, 2015 7:18 AM

All replies

  • http://www.codeproject.com/Articles/88390/MVP-VM-Model-View-Presenter-ViewModel-with-Data-Bi

    Sunday, September 13, 2015 11:46 AM
  • Hi malikmeister,

    this.textBoxOut.Text = strout;

    Change your upper code as below.

    this.textBoxOut.Invoke(new Action(() => { this.textBoxOut.Text = strout; }));
    Best Regards,
    Li Wang
    • Marked as answer by malikmeister Tuesday, September 15, 2015 10:41 AM
    Monday, September 14, 2015 6:50 AM
  • Writing any GUI element from a thread other then the main thread can cause a bajillion issues. Wich is why you were protected from doing it by accident with .NET 2.0. The Cross thread exception is your friend, not your enemy!

    You can prepare the data to be written in another thread. You can retrieve it in that thread (from a file, database or network resources). You can process it in that thread. Even conact it to one singular string (avoid writing to GUI elements in a Loop).
    But the actuall assignment should always be done by the GUI thread via Invoke:

    Invoke

    Normally you would have to deal with ownership on the data in addition, but luckily string is inmutable like the basic types. So you can transmit a string between threads without any wierd side effects.

    • Marked as answer by malikmeister Tuesday, September 15, 2015 10:41 AM
    Monday, September 14, 2015 7:18 AM