gpt4 book ai didi

c# - 'await' 运算符只能与异步 lambda 表达式一起使用

转载 作者:可可西里 更新时间:2023-11-01 08:03:09 24 4
gpt4 key购买 nike

<分区>

我正在尝试将文件列表复制到目录中。我正在使用异步/等待。但是我遇到了这个编译错误

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

我的代码是这样的

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

}

谁能指出我在这里遗漏了什么?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com