调用 self 来绕过不同的线程?C#.

本文关键字:线程 self 调用 | 更新日期: 2023-09-27 18:32:24

我已经环顾四周大约 3 个小时,无法让这个调用工作。我需要调用,因为调用它的内容在不同的线程中并说它不稳定。

这就是我所说的(我这样称呼它textBox1_TextChanged(null, null);):

 private void textBox1_TextChanged(object sender, EventArgs e)
 {
     if(this.InvokeRequired)
     {
        this.Invoke(this?WHAT GOES HERE, null); // I know it should be a delegate or something but I can't change this to that
     }
     else
     {
        string temp = ""; 
        temp += TextToAdd;
        textBox1.Text = "s";
     }
}

调用 self 来绕过不同的线程?C#.

您可以使用 BeginInvoke 从其他线程更新 UI。

if (this.InvokeRequired)
{
  var action = new Action(() => textBox1.Text = "s");
  this.BeginInvoke(action);
}