如何从另一个线程更新 GUI?
发布于 2022-03-22 23:30:17
哪个是Label
从另一个更新 a 的最简单方法Thread
?
- 我有一个
Form
正在运行的thread1
,然后我开始另一个线程(thread2
)。 - 在
thread2
处理一些文件时,我想Label
用Form
当前thread2
工作状态更新 a 。
我怎么能那样做?
关注者
0
被浏览
85
1 个回答
-
最简单的方法是传入匿名方法
Label.Invoke
:// Running on the worker thread string newText = "abc"; form.Label.Invoke((MethodInvoker)delegate { // Running on the UI thread form.Label.Text = newText; }); // Back on the worker thread
请注意,
Invoke
在完成之前会阻塞执行——这是同步代码