AppendText而不是Text to textbox1:避免跨线程操作

时间:2013-11-19 21:17:58

标签: c# multithreading appendtext

我正在使用它将文本写回textbox1,并且它可以正常工作以避免交叉线程操作,但是......我只从它接收一行输出。知道如何更多地进行AppendText通话,而不是基本的文字通话吗?

  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;
        }
    }

1 个答案:

答案 0 :(得分:1)

 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;
     }
}