c# - The 'await' operator can only be used with an async lambda expression -
this question has answer here:
i'm trying copy list of files directory. i'm using async / await. i've been getting compilation error
the 'await' operator can used within async lambda expression. consider marking lambda expression 'async' modifier.
this code looks like
async task<int> copyfilestofolder(list<string> filelist, iprogress<int> progress, cancellationtoken ct) { int totalcount = filelist.count; int processcount = await task.run<int>(() => { int tempcount = 0; foreach (var file in filelist) { string outputfile = path.combine(outputpath, file); await copyfileasync(file, outputfile); //<-- error: compilation error ct.throwifcancellationrequested(); tempcount++; if (progress != null) { progress.report((tempcount * 100 / totalcount))); } } return tempcount; }); return processcount; } private async task copyfileasync(string sourcepath, string destinationpath) { using (stream source = file.open(sourcepath, filemode.open)) { using (stream destination = file.create(destinationpath)) { await source.copytoasync(destination); } } }
pls can point out missing here ?
int processcount = await task.run<int>(() =>
should be
int processcount = await task.run<int>(async () =>
remember lambda shorthand defining method. so, outer method async
, in case you're trying use await
within lambda (which different method outer method). lambda must marked async
well.
Comments
Post a Comment