使用匿名方法调用 WPF 调度程序

发布于 2024-11-14 01:58:34 字数 918 浏览 2 评论 0原文

我刚刚在 C# .Net 4.0 WPF 后台线程中意识到这不起作用(编译器错误):

Dispatcher.Invoke(DispatcherPriority.Normal, delegate()
{
    // do stuff to UI
});

从一些示例中我发现它必须像这样进行转换:(Action)delegate()。然而,在其他示例中,它被转换为其他类,例如 System.Windows.Forms.MethodInvoker。

有人能告诉我上面的例子到底有什么问题吗?我也尝试用其他方法重现它,但它总是在没有强制转换的情况下工作:

delegate void MyAction();
void Method1(MyAction a) {
    // do stuff
}

void Method2(Action a) {
    // do stuff
}

void Tests()
{
    Method1(delegate()
    {
        // works
    });

    Method2(delegate()
    {
        // works
    });

    Method1(() =>
    { 
        // works
    });

    Method2(() =>
    {
        // works
    });

    Method2(new Action(delegate()
    {
        // works
    }));

    new System.Threading.Thread(delegate()
    {
        // works
    }).Start();
}

那么调用调度程序的最佳(最优雅、更少冗余)方法是什么,它有什么特别之处以至于必须强制转换委托?

I just realized in a C# .Net 4.0 WPF background thread that this doesn't work (compiler error):

Dispatcher.Invoke(DispatcherPriority.Normal, delegate()
{
    // do stuff to UI
});

From some examples I found out that it had to be casted like this: (Action)delegate(). However, in other examples it is casted to other classes, e.g. System.Windows.Forms.MethodInvoker.

Can anybody tell me what exactly is wrong with the example above? I also tried to reproduce it with other methods, but it was always working without casting:

delegate void MyAction();
void Method1(MyAction a) {
    // do stuff
}

void Method2(Action a) {
    // do stuff
}

void Tests()
{
    Method1(delegate()
    {
        // works
    });

    Method2(delegate()
    {
        // works
    });

    Method1(() =>
    { 
        // works
    });

    Method2(() =>
    {
        // works
    });

    Method2(new Action(delegate()
    {
        // works
    }));

    new System.Threading.Thread(delegate()
    {
        // works
    }).Start();
}

So whats the best (most elegant, less redundant) way to invoke the Dispatcher, and whats so special with it that delegates must be casted?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧情勿念 2024-11-21 01:58:34

我想指出比 Svick 的代码示例更简洁的代码示例,毕竟我们都喜欢一个衬垫,不是吗?

Dispatcher.Invoke((Action) delegate { /* your method here */ });

I would like to point out even more cleaner code example to Svick's one, after all we all like one liners don't we?

Dispatcher.Invoke((Action) delegate { /* your method here */ });
も让我眼熟你 2024-11-21 01:58:34

查看 Dispatcher.Invoke()。它不需要 Action 或其他特定的委托类型。它采用 Delegate,它是所有委托类型的共同祖先。但是您不能直接将匿名方法转换为该基类型,只能将其转换为某些特定的委托类型。 (这同样适用于 lambda 和方法组。)

为什么需要 Delegate?因为您可以向其传递带有参数或具有返回值的委托。

最干净的方法可能是:

Action action = delegate()
{
    // do stuff to UI
};

Dispatcher.Invoke(DispatcherPriority.Normal, action);

Look at the signature for Dispatcher.Invoke(). It doesn't take Action or some other specific delegate type. It takes Delegate, which is the common ancestor of all delegate types. But you can't convert anonymous method to this base type directly, you can convert it only to some specific delegate type. (The same applies to lambdas and method groups.)

Why does it take Delegate? Because you can pass to it delegates that take parameters or have return values.

The cleanest way is probably:

Action action = delegate()
{
    // do stuff to UI
};

Dispatcher.Invoke(DispatcherPriority.Normal, action);
臻嫒无言 2024-11-21 01:58:34

我使用这个:

Dispatcher.Invoke((Action)(() => YourCodeHere()));

I use this one:

Dispatcher.Invoke((Action)(() => YourCodeHere()));

踏月而来 2024-11-21 01:58:34

“Delegate”类是抽象类,因此您必须向编译器提供从它派生的类型。任何从 Delegate 派生的类都可以,但 Action 通常是最合适的。

The 'Delegate' class is abstract so you have to provide the compiler with a type that is derived from it. Any class derived from Delegate will do but Action is usually the most appropriate one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文