Timurid
Timurid

Reputation: 123

How to run async task without need to await for result in current function/thread?

Is there any chance to avoid waiting? What we want for example:

async Task SomeTask()  
{  
   await ChildTask();

   //Then something we want to be done without waiting till "Child Task" finished  
   OtherWork();  
}  

async Task ChildTask()  
{  
   //some hard work   
}  

Upvotes: 5

Views: 9855

Answers (2)

John Koerner
John Koerner

Reputation: 38087

Capture the Task and then await it after OtherWork is done:

async Task SomeTask()  
{  
   var childTask = ChildTask();

   //Then something we want to be done without waiting till "Child Task" finished  
   OtherWork();  
   await childTask;
}  

Upvotes: 8

Matías Fidemraizer
Matías Fidemraizer

Reputation: 64943

You're not forced to await an asynchronous Task. If you don't await it, it's because you don't care if it finishes successfully or not (fire and forget approach).

If you do so, you shouldn't use the async keyword in your method/delegate signatures.

Upvotes: 6

Related Questions