为什么开销多线程访问WPF UI控件? [英] Why the Overhead for Multi Threaded Access to WPF UI controls?

查看:101
本文介绍了为什么开销多线程访问WPF UI控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我错过了与WPF的问候​​东西(和Windows Forms之前),需要开发多线程应用程序时,写很多重复的代码。在应用程序的每个UI控件最终需要的代码行额外获得和设置每个属性。

Have I missed something with WPF (and Windows Forms before it) in regards to the need to write a lot of repetitive code when developing multi-threaded applications. Every UI control in the application ends up needing extra lines of code to get and set each property.

internal delegate void SetElementIsEnabledDelegate(UIElement element, bool isEnabled);
internal delegate void SetBrushesAndTextDelegate(Brush foregroundBrush, string message);
internal delegate void SetCheckBoxCheckedDelegate(CheckBox checkbox, bool checkedValue);
internal delegate void SetTextBoxTextDelegate(TextBox richTextBox, string text);
internal delegate void SetRichTextBoxTextDelegate(RichTextBox textBox, string text);

internal static void SetElementIsEnabled(UIElement element, bool isEnabled)
{
    if (element.Dispatcher.Thread != Thread.CurrentThread)
    {
        // Execute the same method, but this time on the GUI thread
       element.Dispatcher.Invoke(DispatcherPriority.Normal, new SetElementIsEnabledDelegate(SetElementIsEnabled), element, isEnabled);
       return;
   }

   element.IsEnabled = isEnabled;
}

添加其他30名代表和respecitve方法来表示。

Add another 30 delegates and respecitve methods to that.

据我所知周围有不需要类型安全的方式来完成所有的线程调用。它也好像这是一个可以很容易被引擎盖下的控件库照顾的东西。难道我做错了什么,如果没有为什么微软选择不照顾线程调用的控件?

As far as I'm aware there is no type safe way around the need to do all the thread invoking. It also seems like this is something that could have easily been taken care of under the hood in the control libraries. Am I doing something wrong and if not why would Microsoft choose not to take care of the thread invoking in the controls?

推荐答案

好你当然可以把它简单得多。原因之一是,你不必全部由您自己单独的委托类型 - 您可以使用内置的 Func键< ...> 动作< ; ......方式> 如果你使用.NET 3.5,如果你不是你可以自己声明它们,然后一般使用它们代表

Well you can certainly make it simpler than that. For one thing you don't need all your own separate delegate types - you can use the built-in Func<...> and Action<...> delegates if you're using .NET 3.5, and if you're not you can declare them yourself and then use them generically.

您可以考虑正在写一个辅助方法(作为扩展方法,如果你使用C#3),然后使用匿名方法(或lambda表达式)的另一件事:

The other thing you might consider is writing a helper method (as an extension method, if you're using C# 3) and then use anonymous methods (or lambda expressions):

internal static void SetElementIsEnabled(UIElement element, bool isEnabled)
{
    InvokeIfNecessary(element, delegate
    {
        element.IsEnabled = isEnabled;
    });
}



辅助方法是:

The helper method would be:

public static void InvokeIfNecessary(UIElement element, MethodInvoker action)
{
    if (element.Dispatcher.Thread != Thread.CurrentThread)
    {
        element.Dispatcher.Invoke(DispatcherPriority.Normal, action);
    }
    else
    {
        action();
    }
}



(你或许应该还包括的BeginInvoke 等价的。我一般喜欢的BeginInvoke ,除非我需要的会合。)

(You should probably also include a BeginInvoke equivalent. I generally prefer BeginInvoke unless I need the rendezvous.)

这篇关于为什么开销多线程访问WPF UI控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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