使用多线程为控件分配值 [英] Assigning value to a control in using Multi Threading
本文介绍了使用多线程为控件分配值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是多线程技术的新手,从Google搜索中我获得了很多示例,并且所有示例都在使用控制台应用程序进行解释,但是当我使用Windows窗体时,它不接受为任何控件分配值.显示错误
跨线程操作无效:从不是在其上创建的线程的线程访问的控件"lblInfo"
请帮我.并提供任何有用的链接(带有Winforms的多线程)
在此先感谢
I am new to multi Threadding, From google search I got so many examples and all are explaining with console applications, but when I work with windows forms it is not accepting to assign a value to any control. it is showing error
Cross-thread operation not valid: Control ''lblInfo'' accessed from a thread other than the thread it was created on
Please help me. and provide any useful links (multi threading with winforms)
Thanks in advance
推荐答案
防止跨线程"不允许通过反射操作 [ ^ ]会为您提供帮助.
Prevent ''cross-thread operation not allowed'' exception through reflection[^] would help you.
看看这个例子.在代表的帮助下使用Invoke和BeginInvoke执行该方法.
对Windows窗体控件进行线程安全调用 [
Take a look on this example. Executing the method using Invoke and BeginInvoke with delegates''s aid.
Make Thread-Safe Calls to Windows Forms Controls[^]
我通常会这样做在用户界面代码中:
I usually do something like this in UI code:
private void MoveMyCoolButton(){<br />
if (this.InvokeRequired){<br />
this.Invoke(new Action(MoveMyCoolButton));<br />
}<br />
<...> Continue on your UI code <...><br />
}
这篇关于使用多线程为控件分配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文