调度程序 BeginInvoke 语法
我一直在尝试遵循一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题在于编译器不知道您尝试将 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:
or
Jon Skeet 的回答非常好,但还有其他可能性。我更喜欢“开始调用新动作”,这对我来说很容易阅读和记住。
或
或
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.
or
or
如果您的方法不需要参数,这是我找到的最短版本
If your method does not require parameters, this is the shortest version I've found