2

I am attempting to learn about async programming by following the blog article here. I am running into a compiler error that states I must use a lambda expression with await. The answer is provided here, and here, but I am not grasping how to incorporate the solution into the example from MSDN that I am attempting to work with.

The following error is thrown with this line:

int processed = await UploadAndProcessAsync(image);

The 'await' operator can only be used within an async lambda expression. Consider marking this lambda expression with the 'async' modifier.

From this code:

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.
        }

    }
}
Community
  • 1
  • 1
Dshiz
  • 3,099
  • 3
  • 26
  • 53

2 Answers2

3

This:

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

Will create a delegate using a lambda expression. In order for you to be able to await inside it, it needs to be marked async:

int processCount = await Task.Run<int>(async () => /* process image */)
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • 1
    Thank you - I think this helps me understand what's going on much better. The blog was referencing incomplete code. I'll need to do a bit more study to understand completely. – Dshiz Jun 27 '15 at 18:28
0

It seems that you are getting this exception because you have not implemented the code in UploadAndProcessAsync method. As per your code snippet, I can see, that you are just simply throwing an exception

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