vb.net .Net 中的“InvokeRequired”和“Invoke”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18662905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:58:05  来源:igfitidea点击:

What does 'InvokeRequired' and 'Invoke' mean in .Net?

vb.netinvoke

提问by Nicholas Post

I have been working a lot with threading in a few programs I am working on, and I have always been curious as to what exactly something is doing.

我一直在我正在开发的几个程序中进行大量线程处理,而且我一直很好奇某些东西到底在做什么。

Take for instance the following code, which is ran from a thread to update the UI:

以以下代码为例,该代码从线程运行以更新 UI:

Public Sub UpdateGrid() 
    If Me.InvokeRequired Then 
        Me.Invoke(New MethodInvoker(AddressOf UpdateGrid)) 
    Else 
        DataGridView1.DataSource = dtResults 
        DataGridView1.Refresh() 
        btnRun.Text = "Run Query" 
        btnRun.ForeColor = Color.Black 
    End If 
End Sub

What exactly does the Me.InvokeRequired check for, and what exactly is the Me.Invoke doing? I understand that somehow it gives me access to items on the UI, but how does it accomplish this?

Me.InvokeRequired 到底检查什么,以及 Me.Invoke 到底在做什么?我知道它以某种方式让我可以访问 UI 上的项目,但它是如何实现的?

On a side note, let's say UpdateGrid() was a function that returned a value and had a required parameter. How would I pass the parameter and how would I get the return value after I call the Me.Invoke method? I tried this without the parameter but 'nothing' was getting returned, and I couldn't figure out how to attach the parameter when invoking.

附带说明一下,假设 UpdateGrid() 是一个返回值并具有必需参数的函数。在调用 Me.Invoke 方法后,如何传递参数以及如何获取返回值?我在没有参数的情况下尝试了这个,但是没有返回“什么”,我无法弄清楚如何在调用时附加参数。

回答by OneFineDay

Me.InvokeRequiredis checking to see if it's on the UI thread if not it equals True, Me.Invokeis asking for a delegate to handle communication between the diff threads.

Me.InvokeRequired正在检查它是否在 UI 线程上,如果不等于TrueMe.Invoke则要求委托来处理差异线程之间的通信。

As for your side note. I typically use an event to pass data - this event is still on the diff thread, but like above you can delegate the work.

至于你的旁注。我通常使用一个事件来传递数据——这个事件仍然在 diff 线程上,但像上面一样,你可以委托工作。

Public Sub UpdateGrid() 
    'why ask if I know it on a diff thread
    Me.Invoke(Sub() 'lambda sub
               DataGridView1.DataSource = dtResults 
               DataGridView1.Refresh() 
               btnRun.Text = "Run Query" 
               btnRun.ForeColor = Color.Black 
              End Sub)
End Sub

回答by ProgramFOX

Invoke()makes sure that the invoked method will be invoked on the UI thread. This is useful when you want to make an UI adjustment in another thread (so, not the UI thread).

Invoke()确保调用的方法将在 UI 线程上调用。当您想在另一个线程(而不是 UI 线程)中进行 UI 调整时,这很有用。

InvokeRequiredchecks whether you need to use the Invoke()method.

InvokeRequired检查您是否需要使用该Invoke()方法。

回答by Karl Anderson

From the example you posted, the section that needs to update the UI is part of the Invokelogic, while the retrieval of the data can be done on a worker/background thread.

从您发布的示例中,需要更新 UI 的部分是Invoke逻辑的一部分,而数据的检索可以在工作线程/后台线程上完成。

If Me.InvokeRequired Then

This checks to see if Invoke()is necessary or not.

这会检查是否Invoke()有必要。

Me.Invoke(New MethodInvoker(AddressOf UpdateGrid)) 

This guarantees that this logic will run on the UI thread and since it is handling interacting with the UI (grid), then if you tried to run this on a background thread it would not work.

这保证了此逻辑将在 UI 线程上运行,并且由于它正在处理与 UI(网格)的交互,因此如果您尝试在后台线程上运行它,它将无法正常工作。