C# 如何等待无返回任务方法
我正在学习C# 如何等待无返回任务方法,c#,async-await,C#,Async Await,我正在学习async和wait。我有一个对话框消息的异步方法,我正在等待另一个方法 我的代码=> private async Task CalculateProcess() { dialogCoordinator = DialogCoordinator.Instance; // Show... ProgressDialogController controller = await dialogCoordinator.ShowProgressAsync(this, "HEA
async
和wait
。我有一个对话框消息的异步方法,我正在等待另一个方法
我的代码=>
private async Task CalculateProcess()
{
dialogCoordinator = DialogCoordinator.Instance;
// Show...
ProgressDialogController controller = await dialogCoordinator.ShowProgressAsync(this, "HEADER", "MESSAGE");
controller.SetIndeterminate();
// Do your work...
await testing();//I want to await testing method but i cant await testing because testing method no return task
// Close...
await controller.CloseAsync();
}
private void testing()
{
for(int i=0;i<=1000000;i++)
{
}//Looping is example
}
专用异步任务CalculateProcess()
{
dialogCoordinator=dialogCoordinator.Instance;
//显示。。。
ProgressDialogController=Wait dialogCoordinator.ShowProgressAsync(此“标题”、“消息”);
controller.setUndeterminate();
//做你的工作。。。
wait testing();//我想等待测试方法,但我不能等待测试,因为测试方法没有返回任务
//接近。。。
等待controller.CloseAsync();
}
私人无效测试()
{
对于(int i=0;i如果您真的想等待该方法,这是一种方法:
private async Task CalculateProcess()
{
dialogCoordinator = DialogCoordinator.Instance;
// Show...
ProgressDialogController controller = await dialogCoordinator.ShowProgressAsync(this, "HEADER", "MESSAGE");
controller.SetIndeterminate();
// Do your work...
await testing();
// Close...
await controller.CloseAsync();
}
private Task testing()
{
for(int i=0;i<=1000000;i++)
{
}//Looping is example
return Task.FromResult(0);
}
专用异步任务CalculateProcess()
{
dialogCoordinator=dialogCoordinator.Instance;
//显示。。。
ProgressDialogController=Wait dialogCoordinator.ShowProgressAsync(此“标题”、“消息”);
controller.setUndeterminate();
//做你的工作。。。
等待测试();
//接近。。。
等待controller.CloseAsync();
}
私有任务测试()
{
对于(int i=0;这是另一个“如何异步调用同步方法”,尽管我找不到一个好的副本..这么多..在这种情况下,是什么阻止您更改方法签名?谢谢,但这对我来说是不可理解的。您可以始终将其包装在Task中。运行-详细信息如下:If GetRecord method(从数据库中获取的)是异步的,那么在测试中就不需要Task.FromResult,只需等待GetRecord,如果不是异步的,则调用GetRecord并在测试方法结束时保留Task.FromResult。