您好,我在 C# 中从工作线程调用 richTextBox 时遇到问题。我正在使用 InvokeRequired/Invoke 方法。请看我的代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void ThreadSafe(MethodInvoker method)
{
if (InvokeRequired)
Invoke(method);
else
method();
}
private void WorkerThread(object data)
{
string msg = "\nhello, i am thread " + data.ToString();
ThreadSafe(delegate
{
richTextBox1.AppendText(msg);
});
}
private void button1_Click(object sender, EventArgs e)
{
Thread[] workers = new Thread[3];
for (int i = 0; i < 3; i++)
{
workers[i] = new Thread(WorkerThread);
workers[i].Start(i);
string msg = "\nthread " + i.ToString() + "started!";
richTextBox1.AppendText(msg);
}
int j = 3;
while (j > 0)
{
for (int i = 0; i < 3; i++)
{
Thread.Sleep(250);
richTextBox1.AppendText("\nChecking thread");
if (workers[i].Join(250))
{
string msg = "\nWorker thread " + i.ToString() + " finished.";
richTextBox1.AppendText(msg);
workers[i] = null;
j--; // decrement the thread watch count
}
}
}
}
}
它在富文本框中打印以下内容:
thread 0started!
thread 1started!
thread 2started!
Checking thread
Checking thread
Checking thread
Checking thread
....
它一直在继续,“hello”消息没有被打印出来,UI 被卡住了。 然后我把Invoke()改成了BeginInvoke(),我知道我不应该这样做,结果是这样的:
thread 0started!
thread 1started!
thread 2started!
Checking thread
Worker thread 0 finished.
Checking thread
Worker thread 1 finished.
Checking thread
Worker thread 2 finished.
hello, i am thread 0
hello, i am thread 1
hello, i am thread 2
这是什么原因,我该怎么办?
提前致谢。
最佳答案
Invoke
方法同步运行您的委托(delegate)——它会等到 UI 线程实际运行它,然后再将控制权返回给调用者。
由于 UI 线程正在等待您的线程完成,因此您会遇到死锁。
相比之下,BeginInvoke
方法异步运行您的委托(delegate)——它立即返回,并且委托(delegate)仅在一段时间后 UI 线程空闲时运行。
关于C# - 无法使用 Invoke() 从后台线程追加文本以进行控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6012710/