0

I am learning about async and await. I have an async method for dialog message and I am doing await to another method.

My code=>

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                
}

I want to await to testing method but if I want to use await I need to return task from testing.How can I await testing method if I don't have any other returning task?

FaizanHussainRabbani
  • 3,256
  • 3
  • 26
  • 46
Loran
  • 772
  • 4
  • 16
  • 38
  • https://stackoverflow.com/questions/12144077/async-await-when-to-return-a-task-vs-void – P.N. Feb 21 '18 at 10:20
  • This is another "how to call synchronous method asynchronously", though I can't find a good duplicate.. so many.. – Sinatr Feb 21 '18 at 10:23
  • And what's stopping you from changing the method signature in this case? – user6144226 Feb 21 '18 at 10:24
  • Thanks,but this is not understandable for me. – Loran Feb 21 '18 at 10:24
  • You could always wrap this in Task.Run - details to follow:https://stackoverflow.com/questions/13071833/is-wrapping-a-synchronous-call-in-a-task-run-to-make-it-asynchronous-beneficia – user6144226 Feb 21 '18 at 10:25
  • When i google,i see many example are using Task.Delay(10000) and return this.If i dont have Task ,How can i use await this. – Loran Feb 21 '18 at 10:27
  • @user6144226 yep i was try with Task.Run(() => testing()).But when i try this code,this is not await and call directly controller.CloseAsync(); – Loran Feb 21 '18 at 10:28
  • Why do you even want to await this? if you just call testing() without await program will execute this method synchronously. – zhuber Feb 21 '18 at 10:28
  • if testing method return Task,my problem will solve but I dont have Task in testing method. – Loran Feb 21 '18 at 10:29
  • @zhuber no if i call directly,it will not await and dialog will close immediately. – Loran Feb 21 '18 at 10:31
  • await Task.Run(()=>DoStuff()) is perfectly valid syntax wise. – user6144226 Feb 21 '18 at 10:32
  • @Loran it will not await but since testing method will execute pretty fast, await controller.CloseAsync(); will be called shortly after and thats why it closes immediatelly. Maybe post what exactly is inside testing() method? – zhuber Feb 21 '18 at 10:35
  • @zhuber, GetRecord from database will be inside of testing method and it take 10 sec.Thanks for your advice and i was not test with GetRecord .I will let you known after testing with real data. – Loran Feb 21 '18 at 10:41

1 Answers1

1

If you really want to await that method this is the way to go:

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);
}
zhuber
  • 5,364
  • 3
  • 30
  • 63
  • If GetRecord method (which fetches from database) is async, then you won't need Task.FromResult in testing, just await GetRecord, and if its not async then call GetRecord and leave Task.FromResult at the end of testing method. – zhuber Feb 21 '18 at 10:54