c#表单在新线程启动时挂起

时间:2018-04-07 10:59:36

标签: c# multithreading

我创建了新线程并启动它! 当新线程运行时,Winform挂起(冻结)
为什么winform挂起?
我希望WinForm在我开始新线程时自由移动 怎么做?
(我不会在这里使用线程池。)

        private void button4_Click(object sender, EventArgs e)
        {
            Thread t1 = new Thread(new ThreadStart(delegate ()
            {
                Run();
            }));
            t1.Start();
            t1.Join();
            MessageBox.Show("Complete");
        }

        private void Run()
        {
            int a = 1;
            for (int i = 1; i <= 999999999; i++)
            {
                ++a;
            }
        }

1 个答案:

答案 0 :(得分:3)

Read the documentation about Join

  

阻止调用线程,直到由此表示的线程   实例终止。

所以,你启动线程,然后你加入,这意味着你的UI线程将阻塞并等待线程完成。

你可以async/await

private async void button4_Click(object sender, EventArgs e)
{
    await Task.Run(Run);
    MessageBox.Show("Complete");
}
相关问题