Search code examples
c#multithreadingappendtext

AppendText rather than Text to textbox1: avoiding cross thread operation


I'm using this to write text back into textbox1, and it works correctly to avoid cross thread operation, but... I only receive a single line of output from it. Any idea how I can do more of an AppendText call rather than a basic text call?

  private void SetText(string text)
    {
        // InvokeRequired required compares the thread ID of the
        // calling thread to the thread ID of the creating thread.
        // If these threads are different, it returns true.
        if (this.textBox1.InvokeRequired)
        {
            SetTextCallback d = new SetTextCallback(SetText);
            this.Invoke(d, new object[] { text });
        }
        else
        {
            this.textBox1.Text = text;
        }
    }

Solution

  •  private void SetText(string text)
     {
        // InvokeRequired required compares the thread ID of the
         // calling thread to the thread ID of the creating thread.
          // If these threads are different, it returns true.
        if (this.textBox1.InvokeRequired)
          {
            SetTextCallback d = new SetTextCallback(SetText);
            this.Invoke(d, new object[] { text });
         }
        else
         {
             //append  text  like  this  
             this.textBox1.Text += text;
         }
    }