wpf 如何使用 InvokeRequired

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15580494/
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-13 08:16:17  来源:igfitidea点击:

How to use InvokeRequired

c#wpfvb.net

提问by iAteABug_And_iLiked_it

I have the code below in vb.net, and I am converting it to c#. EDITEdited out irrelevant code.

我在 vb.net 中有下面的代码,我正在将其转换为 c#。 编辑编辑掉不相关的代码。

but I am stuck at the one with InvokeRequired. I have added a reference to System.Windows.Formsand still the code completion doesn't show InvokeRequired. The listview is on a separate thread to the one I am calling it from, and I need to get around that using Invoke. Can you please tell me what am I missing? Thank you.

但我被困在了InvokeRequired. 我已经添加了一个引用System.Windows.Forms,但代码完成仍然没有显示InvokeRequired。列表视图位于与我调用它的线程不同的线程上,我需要使用 Invoke 解决该问题。你能告诉我我错过了什么吗?谢谢你。

    Delegate Sub _AddClient(ByVal client As Socket)
    Private Sub AddClient(ByVal client As Socket)
        If InvokeRequired Then
            Invoke(New _AddClient(AddressOf AddClient), client)
            Exit Sub
        End If
        Dim lvi As New ListViewItem(client.LocalEndPoint.ToString)
        lvi.Tag = client
        lsvClients.Items.Add(lvi)
    End Sub

回答by Federico Berasategui

Dude, if you're working with WPF, remove all references to System.Windows.Forms, WPF doesn't need, nor care about that, and you'd better not be confused with classes with equal names from different namespaces and frameworks (for instance System.Windows.Forms.Controland System.Windows.Control.

伙计,如果您正在使用 WPF,请删除对 的所有引用System.Windows.Forms,WPF 不需要,也不关心它,并且您最好不要与来自不同命名空间和框架的具有相同名称的类(例如System.Windows.Forms.ControlSystem.Windows.Control.

InvokeRequired()does not exist in WPF, that's replaced by Dispatcher.CheckAccess().

InvokeRequired()WPF 中不存在,而是由Dispatcher.CheckAccess().

回答by Hanlet Esca?o

Try this:

尝试这个:

Delegate Sub _AddClient(ByVal client As Socket)
Private Sub AddClient(ByVal client As Socket)
    If ListView1.InvokeRequired Then
        Invoke(New _AddClient(AddressOf AddClient), client)
        Exit Sub
    End If
    Dim lvi As New ListViewItem(client.LocalEndPoint.ToString)
    lvi.Tag = client
    ListView1.Items.Add(lvi)
End Sub

回答by Pieter Geerkens

Here is an example of how it is used:

下面是一个如何使用它的例子:

protected void InvokePathDone(Task<IPath<ICoordsCanon>> task,
                              Action<Task<IPath<ICoordsCanon>>> action) {
  if (InvokeRequired)  Invoke(action, task);
  else                 action(task);
}

Update for OP question below:

更新以下 OP 问题:

InvokeRequired()is defined for the Control class to enable callbacks from event delegates to determine if they are, or are not, on the UI thread, and thus whether they must, or must not, perform their action under Invoke().

InvokeRequired()为 Control 类定义以启用来自事件委托的回调,以确定它们是否在 UI 线程上,从而确定它们是否必须或不得在 Invoke() 下执行其操作。