调用 ToolStripMenuItem
我想弄清楚是否有办法调用 ToolStripMenuItem。
例如,当返回结果时,我正在调用Web服务(异步)。我根据结果填充下拉项,(在回调方法中)
ToolStripMenuItem.DropDownItems.Add(new ToolStripItemEx("start"));
但我得到异常
跨线程操作无效:控制''访问来自创建它的线程以外的线程。
没有与工具条项关联的调用函数, 我还有其他方法可以做到这一点吗?我是否试图以完全错误的方式做到这一点?任何输入都会有帮助。
I'm trying to figure out if there's a way to Invoke ToolStripMenuItem.
For example,I am calling a web service(ASynchrously) when result is returned.i populate drop down items according to result,(In call back method)
ToolStripMenuItem.DropDownItems.Add(new ToolStripItemEx("start"));
but I get exception
Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.
There is no invoke function associated with the toolstrip item,
Is there another way I can do this? Am I trying to do this the completely wrong way? Any input would be helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在尝试在另一个线程中执行依赖于控件主线程的代码,您应该使用
Invoke
方法调用它:当从与控件最初创建的线程不同的线程访问控件成员/方法时上,您应该使用
control.Invoke
方法,它将把invoke委托中的执行编组到主线程。编辑:由于您使用的是
ToolStripMenuItem
而不是ToolStrip
,因此ToolStripMenuItem
没有Invoke
code> 成员,因此您可以通过“this.Invoke
”使用表单调用,也可以使用toolStrip
其父级“ToolStrip
”调用,因此:You are trying to execute code that rely on control main thread in another thread, You should call it using
Invoke
method:When accessing controls members/methods from a thread that is different from thread that the control originally created on, you should use
control.Invoke
method, it will marshal the execution in the delegate of invoke to the main thread.Edit: Since you are using
ToolStripMenuItem
notToolStrip
, theToolStripMenuItem
doesn't haveInvoke
member, so you can either use the form invoke by "this.Invoke
" or yourtoolStrip
its parent "ToolStrip
" Invoke, so:您正在尝试从线程而不是主线程访问菜单项,因此请尝试以下代码:
You are trying to access the menu item from a thread rather than the main thread, so try out this code: