C#,WPF,需要时自动调用Dispatcher.Invoke?

时间:2022-08-28 07:39:36

I have a program with a Geospace map embedded into it. The event handling for the map is handled on a separate thread to keep the map responsive (for example, the events that fire when the map is clicked).

我有一个嵌入了Geospace地图的程序。地图的事件处理在单独的线程上处理,以使地图保持响应(例如,单击地图时触发的事件)。

The problem I am having is when the map fires an event, my program needs to update some things in it's gui, and also call back into the map to handle placing pictures on the map.

我遇到的问题是当地图触发一个事件时,我的程序需要更新它的gui中的一些东西,并且还回调到地图以处理在地图上放置图片。

I tried wrapping the entire event handler method in this.Dispatcher.Invoke, which puts me back on the main UI thread. This works great for updating my GUI, but when i call back into the map, I'm still on the UI thread which can cause some problems in the map.

我尝试在this.Dispatcher.Invoke中包装整个事件处理程序方法,这使我回到主UI线程。这对于更新我的GUI很有用,但是当我回调到地图时,我仍然在UI线程上,这可能会导致地图中出现一些问题。

Basically, in order to make this work, I'm going to have to run dispatcher.invoke each time I want to change a control on my gui. Is there a way I can automatically do this without wrapping each call in dispatcher.invoke? I hope this all makes sense.

基本上,为了使这项工作,每次我想要更改我的gui上的控件时,我将不得不运行dispatcher.invoke。有没有办法我可以自动执行此操作而不在dispatcher.invoke中包装每个调用?我希望这一切都有道理。

Heres some example code for the event I'm talking about..

下面是我正在谈论的事件的一些示例代码..

    private void Map_OnMapClicked(object sender, MapClickedEventArgs e)
    {
        this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
        {
            // Do something to update my gui
        }));

        Map.DoSomethingInTheMap();

        this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
        {
            // Do something to update my gui
        }));


        //etc etc etc
    }

2 个解决方案

#1


6  

If you need to keep each respective operation in its own synchronization context, this is unfortunately the best approach. You'll need to Invoke using the Dispatcher whenever you update your GUI.

如果您需要将每个相应的操作保留在自己的同步上下文中,那么这是最好的方法。每当更新GUI时,都需要使用Dispatcher进行调用。

Here are a couple of suggestions for making this easier:

以下是一些使这更容易的建议:

  1. Try to batch your GUI operations. In addition to requiring less code (via less invoke calls), you'll get better performance. Each Dispatcher.Invoke call carries a fair amount of overhead, since it posts a message into the Dispatcher's message queue which must be processed.

    尝试批量处理GUI操作。除了需要更少的代码(通过更少的调用调用),您将获得更好的性能。每个Dispatcher.Invoke调用都会产生相当大的开销,因为它会将消息发布到Dispatcher的消息队列中,并且必须对其进行处理。

  2. Consider using Dispatcher.BeginInvoke to avoid blocking, unless you really need to wait.

    考虑使用Dispatcher.BeginInvoke来避免阻塞,除非你真的需要等待。

  3. If you can use the Task Parallel Library from .NET 4 (or the backport to 3.5sp1 in the Rx Framework), you might want to consider reworking this to use Task instances synchronized to the GUI thread. By creating a TaskScheduler using FromCurrentSynchronizationContext, you can schedule tasks to run on the GUI easier than the Dispatcher Invoke calls. This also can give you some control over batching, since you can schedule them, and block/wait as needed, very easily.

    如果您可以使用.NET 4中的任务并行库(或Rx Framework中的backport到3.5sp1),您可能需要考虑重新处理它以使用与GUI线程同步的Task实例。通过使用FromCurrentSynchronizationContext创建TaskScheduler,您可以安排在GUI上运行的任务比Dispatcher Invoke调用更容易。这也可以让您对批处理有一些控制权,因为您可以非常轻松地安排它们,并根据需要阻止/等待。

#2


2  

You could use something like PostSharp or try to condense your UI updates to single method calls where you invoke once and do a lot. Or even a pattern like this (it's Winforms but the idea is the same):

你可以使用像PostSharp这样的东西,或者尝试将你的UI更新压缩到你调用一次并做很多事情的单个方法调用。甚至是这样的模式(它是Winforms,但想法是一样的):

private void UpdateToolStripItemText(ToolStripItem toolStripItem, string text)
{
    if (InvokeRequired)
    {
        Invoke(new UpdateToolStripItemTextDelegate(UpdateToolStripItemText), new object[] { toolStripItem, text });
    }
    else
    {
        if (text != null)
        {
            toolStripItem.Text = text;
        }
    }
}

#1


6  

If you need to keep each respective operation in its own synchronization context, this is unfortunately the best approach. You'll need to Invoke using the Dispatcher whenever you update your GUI.

如果您需要将每个相应的操作保留在自己的同步上下文中,那么这是最好的方法。每当更新GUI时,都需要使用Dispatcher进行调用。

Here are a couple of suggestions for making this easier:

以下是一些使这更容易的建议:

  1. Try to batch your GUI operations. In addition to requiring less code (via less invoke calls), you'll get better performance. Each Dispatcher.Invoke call carries a fair amount of overhead, since it posts a message into the Dispatcher's message queue which must be processed.

    尝试批量处理GUI操作。除了需要更少的代码(通过更少的调用调用),您将获得更好的性能。每个Dispatcher.Invoke调用都会产生相当大的开销,因为它会将消息发布到Dispatcher的消息队列中,并且必须对其进行处理。

  2. Consider using Dispatcher.BeginInvoke to avoid blocking, unless you really need to wait.

    考虑使用Dispatcher.BeginInvoke来避免阻塞,除非你真的需要等待。

  3. If you can use the Task Parallel Library from .NET 4 (or the backport to 3.5sp1 in the Rx Framework), you might want to consider reworking this to use Task instances synchronized to the GUI thread. By creating a TaskScheduler using FromCurrentSynchronizationContext, you can schedule tasks to run on the GUI easier than the Dispatcher Invoke calls. This also can give you some control over batching, since you can schedule them, and block/wait as needed, very easily.

    如果您可以使用.NET 4中的任务并行库(或Rx Framework中的backport到3.5sp1),您可能需要考虑重新处理它以使用与GUI线程同步的Task实例。通过使用FromCurrentSynchronizationContext创建TaskScheduler,您可以安排在GUI上运行的任务比Dispatcher Invoke调用更容易。这也可以让您对批处理有一些控制权,因为您可以非常轻松地安排它们,并根据需要阻止/等待。

#2


2  

You could use something like PostSharp or try to condense your UI updates to single method calls where you invoke once and do a lot. Or even a pattern like this (it's Winforms but the idea is the same):

你可以使用像PostSharp这样的东西,或者尝试将你的UI更新压缩到你调用一次并做很多事情的单个方法调用。甚至是这样的模式(它是Winforms,但想法是一样的):

private void UpdateToolStripItemText(ToolStripItem toolStripItem, string text)
{
    if (InvokeRequired)
    {
        Invoke(new UpdateToolStripItemTextDelegate(UpdateToolStripItemText), new object[] { toolStripItem, text });
    }
    else
    {
        if (text != null)
        {
            toolStripItem.Text = text;
        }
    }
}