C# - 无法使用 Invoke() 从后台线程追加文本以进行控制

标签 c# multithreading user-interface controls invoke

您好,我在 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/

相关文章:

c# - 如何使用表达式 linq 创建函数

c# - 将 SQL Server Serilog 接收器包装在 Async 中是否有意义?

c++ - 如何在 Qt 的主事件循环中使用 std::thread?

java - 如何删除 Android 网格布局之间的空间

c# - 通过 visual studio 运行的 UI 外观与安装的应用程序不同

c# - 将脏 Excel 2007 文件导入并清理到 SQL Server 2005

c# - 修改nHibernate在nServiceBus中保存Saga数据的方式

java - 当容器关闭时 sleep 线程会发生什么?

c# - 为什么并发修改数组这么慢?

ios - UIButton正在移动...它不应移动