I've this code:
static void Main(string[] args)
{
// start import process
Task<int> task = StartImportProcess();
task.Wait();
// check result
// process finished
Console.ReadKey();
}
static async Task<int> StartImportProcess()
{
int result = 0;
result = await ImportCustomers();
// some other async/await operations
return result;
}
static Task<int> ImportCustomers()
{
// some heavy operations
Thread.Sleep(1000);
return 1; // <<< what should I return?
}
使用 Task
和 async/await
。我想要将 int
作为任务的结果返回。应该返回哪个对象呢?return 1;
是行不通的。
return Task.FromResult(1)
的意思是返回一个已经完成并且结果为1的任务。关于Task.FromResult
的更多信息请参考这里。 - CodeNotFound