调度语法的BeginInvoke [英] Dispatcher BeginInvoke Syntax

查看:149
本文介绍了调度语法的BeginInvoke的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图遵循一些WCF数据服务的实例并具有以下代码:

I have been trying to follow some WCF Data Services examples and have the following code:

private void OnSaveCompleted(IAsyncResult result)
    {
        Dispatcher.BeginInvoke(() =>
        {
            context.EndSaveChanges(result);
        });
    }



其中由下面称为:

Which is called by the following:

this.context.BeginSaveChanges(SaveChangesOptions.Batch, this.OnSaveCompleted, null);

现在我开始有点困惑在这里。首先,代码的第一位是显示的语法错误参数类型lambda表达式是不能分配给参数类型System.Delegate。因此,而不是一味地试图按照示例代码我试图了解发生了什么怎么回事。不幸的是我奋力理解的错误加上什么是真正发生的事情。

Now I am getting a little confused here. Firstly, the first bit of code is showing a syntax error of "Argument type lambda expression is not assignable to parameter type System.Delegate". So instead of blindly trying to follow the example code I tried to understand what was going on here. Unfortunately I am struggling to understand the error plus what is actually happening.

我觉得有点愚蠢,因为我相信这是容易的。

I feel a bit stupid as I am sure this is easy.

在提前任何启迪!

推荐答案

问题是,编译器不知道什么感谢一种委托你想lambda表达式转换为。您可以修复,要么用投,或者一个独立的变量:

The problem is that the compiler doesn't know what kind of delegate you're trying to convert the lambda expression to. You can fix that either with a cast, or a separate variable:

private void OnSaveCompleted(IAsyncResult result)
{        
    Dispatcher.BeginInvoke((Action) (() =>
    {
        context.EndSaveChanges(result);
    }));
}

private void OnSaveCompleted(IAsyncResult result)
{
    Action action = () =>
    {
        context.EndSaveChanges(result);
    };
    Dispatcher.BeginInvoke(action);
}

这篇关于调度语法的BeginInvoke的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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