C# 遵循MSDN博客异步进度示例时出现Lamba错误&;取消

C# 遵循MSDN博客异步进度示例时出现Lamba错误&;取消,c#,.net,asynchronous,lambda,async-await,C#,.net,Asynchronous,Lambda,Async Await,我试图通过阅读这篇博客文章来了解异步编程。我遇到一个编译器错误,该错误说明我必须将lambda表达式与wait一起使用。答案已经提供了,但是我没有掌握如何将解决方案合并到我尝试使用的MSDN示例中 此行将引发以下错误: int processed = await UploadAndProcessAsync(image); “await”运算符只能在异步lambda表达式中使用。考虑用“AsiNC”修饰符标记lambda表达式。 根据该代码: using System; using System

我试图通过阅读这篇博客文章来了解异步编程。我遇到一个编译器错误,该错误说明我必须将lambda表达式与wait一起使用。答案已经提供了,但是我没有掌握如何将解决方案合并到我尝试使用的MSDN示例中

此行将引发以下错误:

int processed = await UploadAndProcessAsync(image);
“await”运算符只能在异步lambda表达式中使用。考虑用“AsiNC”修饰符标记lambda表达式。

根据该代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;

namespace AsyncAttempt
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        async Task<int> UploadPicturesAsync(List<Image> imageList, IProgress<int> progress)
        {
            int totalCount = imageList.Count;
            int processCount = await Task.Run<int>(() =>
            {
                int tempCount = 0;
                foreach (var image in imageList)
                {
                    //await the processing and uploading logic here
                    int processed = await UploadAndProcessAsync(image);
                    if (progress != null)
                    {
                        progress.Report((tempCount * 100 / totalCount));
                    }
                    tempCount++;
                }

                return tempCount;
            });
            return processCount;
        }

        private Task<int> UploadAndProcessAsync(Image image)
        {
            throw new NotImplementedException();
        }

        private async void Start_Button_Click(object sender, RoutedEventArgs e)
        {
            //construct Progress<T>, passing ReportProgress as the Action<T> 
            var progressIndicator = new Progress<int>(ReportProgress);
            //call async method
            int uploads = await UploadPicturesAsync(GenerateTestImages(), progressIndicator);
        }

        private List<Image> GenerateTestImages()
        {
            throw new NotImplementedException();
        }

        void ReportProgress(int value)
        {
            //Update the UI to reflect the progress value that is passed back.
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用系统线程;
使用System.Threading.Tasks;
使用System.IO;
使用System.Windows.Forms;
命名空间异步尝试
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
异步任务上载图片同步(列表imageList,IProgress progress)
{
int totalCount=imageList.Count;
int processCount=等待任务。运行(()=>
{
int tempCount=0;
foreach(imageList中的var图像)
{
//在此等待处理和上传逻辑
int processed=wait UploadAndProcessAsync(图像);
如果(进度!=null)
{
进度报告((临时计数*100/总计数));
}
tempCount++;
}
返回tempCount;
});
返回进程计数;
}
专用任务UploadProcessAsync(映像)
{
抛出新的NotImplementedException();
}
专用异步无效开始按钮单击(对象发送方,路由目标)
{
//构造进度,将ReportProgress作为操作传递
var progressIndicator=新进度(报告进度);
//调用异步方法
int uploads=等待上传图片同步(GenerateTestImages(),progressIndicator);
}
私有列表GenerateEstimates()
{
抛出新的NotImplementedException();
}
void ReportProgress(int值)
{
//更新UI以反映传回的进度值。
}
}
}

您似乎得到了这个异常,因为您没有在UploadProcessAsync方法中实现代码。根据您的代码片段,我可以看出,您只是简单地抛出了一个异常

private Task<int> UploadAndProcessAsync(Image image)
    {
        throw new NotImplementedException();
    }
private Task UploadAndProcessAsync(图像)
{
抛出新的NotImplementedException();
}
这是:

int processCount = await Task.Run<int>(() => /* process image */)

这不是一个例外,这是一个编译器错误,它与未实现的方法无关谢谢-我认为这有助于我更好地理解正在发生的事情。该博客引用了不完整的代码。我需要做更多的研究才能完全理解。
int processCount = await Task.Run<int>(async () => /* process image */)