调度程序 BeginInvoke 语法

发布于 2024-09-24 22:52:05 字数 562 浏览 3 评论 0原文

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

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

由以下内容调用:

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

现在我在这里有点困惑。首先,代码的第一位显示语法错误

参数类型 lambda 表达式不可分配给参数类型 System.Delegate

因此,我没有盲目地尝试遵循示例代码,而是尝试了解这里发生的情况。不幸的是,我正在努力理解错误以及实际发生的情况。谁能解释一下吗?

我觉得有点愚蠢,因为我确信这很容易。

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);

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. Can anyone explain?

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

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

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

发布评论

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

评论(3

伴我老 2024-10-01 22:52:05

问题在于编译器不知道您尝试将 lambda 表达式转换为哪种委托。您可以通过强制转换或单独的变量来修复该问题:

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

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

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);
    }));
}

or

private void OnSaveCompleted(IAsyncResult result)
{
    Action action = () =>
    {
        context.EndSaveChanges(result);
    };
    Dispatcher.BeginInvoke(action);
}
一枫情书 2024-10-01 22:52:05

Jon Skeet 的回答非常好,但还有其他可能性。我更喜欢“开始调用新动作”,这对我来说很容易阅读和记住。

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

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

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

Answer by Jon Skeet is very good but there are other possibilities. I prefer "begin invoke new action" which is easy to read and to remember for me.

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

or

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

or

private void OnSaveCompleted(IAsyncResult result)
{       
    Dispatcher.BeginInvoke(new Action(() => context.EndSaveChanges(result)));
}
鹊巢 2024-10-01 22:52:05

如果您的方法不需要参数,这是我找到的最短版本

Application.Current.Dispatcher.BeginInvoke((Action)MethodName); 

If your method does not require parameters, this is the shortest version I've found

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