Task.Run(() =>
{
Thread.Sleep(4000);
string sPower = getPower();//耗时操作一定要放在BeginInvoke的外面
this.BeginInvoke(new Action(() =>
{
lbCurrentPower.Text = sPower;
}));
});
string sPower = getPower();这一句是耗时操作,一定要放在BeginInvoke的外面,如果放在BeginInvoke或invoke的里面,则程序界面依然会被阻塞。
本文介绍了一种防止UI线程被长时间操作阻塞的方法。通过使用Task.Run将耗时操作移出主线程,并利用BeginInvoke回调更新UI,确保了用户界面的流畅体验。
730

被折叠的 条评论
为什么被折叠?



