关于(Me.InvokeInvokeRequired)帮助的清晰度 [英] Clarity on (Me.InvokeInvokeRequired)Help

查看:216
本文介绍了关于(Me.InvokeInvokeRequired)帮助的清晰度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我一直试图理解以下代码行,但我没有得到它可能有人可能能够解释下面的代码(Me.Invoke / InvokeRequired)



谢谢

WJ








I have been trying to understand the following lines of code, but I am not getting it maybe someone might be able to explain the code below(Me.Invoke/InvokeRequired)

Thanks
WJ



Private Sub ReceivedText(ByVal [text] As String)
        'compares the ID of the creating Thread to the ID of the calling Thread
        If Me.rtbReceived.InvokeRequired Then
            Dim x As New SetTextCallback(AddressOf ReceivedText)
            Me.Invoke(x, New Object() {(text)})
        Else
            Me.rtbReceived.Text &= [text]
        End If
    End Sub

推荐答案

当然。此属性 System.Windows.Forms.Control.InvokeRequired 只是检查调用线程是否与UI线程相同。哪个UI线程?在某些线程中,您调用了 System.Windows.Forms.Application.Run(System.Windows.Forms.Form)。几乎总是,这是应用程序的起始(主)线程,并以入口点方法( Main )启动应用程序。稍后,您将一些控件添加到同一个应用程序,然后调用 InvokeRequired 。如果从save it thread调用此属性,则显然不需要调用,因为您可以在同一个线程中调用与相同UI相关的任何内容。如果这是任何其他线程,则需要调用。



您需要致电这家酒店吗?尽管普遍相信,通常不会。通常你设计代码的方式是某些方法被设计为从其他线程调用,所以你需要使用 Invoke BeginInvoke 立即,没有任何检查。 (即使调用线程是UI线程,它也会工作。)只有在极少数情况下,您希望开发一个可以在UI和非UI线程中调用的通用方法,然后这个检查很有用。但并不是真的很关键。



你能否相反,在没有任何调用的情况下调用一些与UI相关的方法?如果你是从非UI线程调用它,没办法!从来没有尝试过。您不能从非UI线程调用与UI相关的任何内容。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

使用Treeview扫描仪和MD5的问题 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何让keydown事件在不同的操作上运行vb.net中的线程 [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。



-SA
Sure. This property, System.Windows.Forms.Control.InvokeRequired simply checks up if the calling thread is the same as the UI thread. Which UI thread? In some thread, you called System.Windows.Forms.Application.Run(System.Windows.Forms.Form). Almost always, this is the starting (main) thread of your application, and you start the application in your entry-point method (Main). Later on, you add some control to the same application, and later call InvokeRequired. If this property is called from the save very thread, invoke is apparently not required, as you can call anything related to the "same UI" in the same thread. If this is any other thread, invoke is required.

Do you need to call this property? Despite common believe, usually not. Usually you design your code the way that some methods are designed to be called from some other thread, so you need to use Invoke or BeginInvoke immediately, without any checks. (It will work even if the calling thread is the UI thread.) Only in some rare cases, you want to develop an "universal" method which can be called in both UI and non-UI thread, then this check is useful. But not really critical.

Can you do the opposite, call some UI-related method without any invocation? If you are calling it from non-UI thread, no way! Never even try. You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


这篇关于关于(Me.InvokeInvokeRequired)帮助的清晰度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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