运行外部线程需要调用吗? [英] Invoke required to run an external thread?

查看:76
本文介绍了运行外部线程需要调用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

好的,所以我被告知,如果不使用调用,就无法运行VB.net中另一个程序创建的线程?这是真的?我相信这也有助于线程变得线程安全吗?!谁能解释这个问题或提出运行此线程的更好方法?

谢谢,
James

Hello everyone.

Ok so I have been told that you cannot run a thread created by another program in VB.net without using an invoke? Is this true? I believe this also helps the thread to become thread safe?! Can anyone explain this or suggest a better way of running this thread?

Thanks,
James

推荐答案

这仅与UI有关.

您无法通过非UI线程在UI中调用任何内容.您可以使用System.Threading.DispatcherBeginInvokeInvoke(首选使用表格和WPF)或System.Windows.Forms.Control(显然仅使用表格).

请参阅我过去的回答中的详细说明和示例代码:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

如果您想对通用(非UI)线程执行类似的操作,则需要付出特别的努力.实际上,我实现了一种非常相似的调用机制.

请参阅我的提示/技巧文章,以获取完整的源代码,详细的说明和用法示例:
用于线程通信和线程间调用的简单阻塞队列 [ ^ ].

—SA
This is related exclusively to the UI.

You cannot call anything in UI from a non-UI thread. You can use BeginInvoke or Invoke of System.Threading.Dispatcher (both forms and WPF, preferred) or System.Windows.Forms.Control (Forms only, apparently).

See detailed explanation and samples code in my past asnwers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

More links on thread use:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

If you want to do something like that to a general-purpose (non-UI) thread, it needs a special effort. I actually implemented a very similar invocation mechanism.

Please see my Tips/Tricks article here for a full source code, detailed explanation and usage samples:
Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

—SA


这篇关于运行外部线程需要调用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆