【C#】多线程解决UI界面卡死的问题

时间:2024-01-06 17:44:32

一个经典的例子:


问题:

都说Invoke是同步的,BeginInvoke是异步的,但为何用BeginInvoke做耗时操作依然会卡死UI?

http://www.cnblogs.com/blosaa/archive/2013/05/30/3107381.html

小结:

BeginInvoke的异步是指相对于调用BeginInvoke的线程异步,而不是相对于UI线程异步。所以在UI线程调用BeginInvoke依然会卡死UI。  

解决办法是开一个子线程,耗时操作在该子线程中完成,完成后在子线程中调用BeginInvoke交由UI线程更新界面,而不是在UI线程中调用BeginInvoke。

注意,记好Control的Invoke和BeginInvoke都执行在主线程即UI线程上!(这是Winform的情况,WPF中交由Control所关联的Dispatcher来处理,Control身上没有Invoke和BeginInvoke)


测试发现,C#的主线程(UI线程)默认是未命名的,Name属性为null。想要知道获得的线程是否为主线程,可以在Visual Studio的【线程】窗口中看到,如下图。既然不知道线程的Name,那么还可以通过线程的托管ID来知晓。

【C#】多线程解决UI界面卡死的问题

Thread objThread = new Thread(new ThreadStart(delegate
{
Console.WriteLine("当前线程ID = " + Thread.CurrentThread.ManagedThreadId); // 是子线程
Console.WriteLine("当前线程ID = " + Application.Current.Dispatcher.Thread.ManagedThreadId); // 是UI线程
}));

关于使用ManualResetEvent使线程阻塞与继续: