我收到警告"这种异步方法缺乏等待'等等。运营商并将同步运行。考虑使用' await'运营商等待非阻塞API调用,或等待Task.Run(...)'在后台线程上进行CPU绑定的工作。虽然听起来很明显,但我认为我被覆盖了,因为我运行了所有进程,并在我的processFiles方法中有一个Await Task.WhenAll()。我需要以不同的方式做这件事吗?任何想法都会受到赞赏。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace ProcessFiles
{
public partial class ProcessFiles : Form
{
public ProcessFiles(int userLevel, int userID)
{
}
private void btnProcess_Click(object sender, EventArgs e)
{
processFiles();
}
async Task<int> processFiles()
{
var processingTasks = new List<Task>();
foreach (string fileName in listBox1.Items)
{
processingTasks.Add(process012(fileName));
processingTasks.Add(process123(fileName));
processingTasks.Add(process234(fileName));
}
await Task.WhenAll(processingTasks);
return (1);
}
async Task<int> process173(string fileName)
{
return (retVal);
}
async Task<int> process032(string fileName)
{
return (retVal);
}
async Task<int> process018(string fileName)
{
return (retVal);
}
答案 0 :(得分:2)
不需要在processXXX方法中使用async Task只返回Task&lt; int&gt;代替。您将等待主processFiles()方法中的任务:
private async void btnProcess_Click(object sender, EventArgs e)
{
await processFiles();
}
async Task<int> processFiles()
{
var processingTasks = new List<Task>();
foreach (string fileName in listBox1.Items)
{
processingTasks.Add(process012(fileName));
processingTasks.Add(process123(fileName));
processingTasks.Add(process234(fileName));
}
await Task.WhenAll(processingTasks);
return (1);
}
Task<int> process173(string fileName)
{
return Task.FromResult(retVal)
}
Task<int> process032(string fileName)
{
return Task.FromResult(retVal)
}
Task<int> process018(string fileName)
{
return Task.FromResult(retVal)
}