5

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   
}  
Timurid
  • 123
  • 2
  • 6
  • 1
    Why use `Async` and `Await` in the first place then? Just run ChildTask as a normal task using `Task.Run()`. – Jens Mar 21 '15 at 20:10

2 Answers2

8

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;
}  
John Koerner
  • 37,428
  • 8
  • 84
  • 134
  • I think this is not a good answer because here at the question it states that we dont need the result of the child task but in your solution the thread comes back to the await when the task is finished. – Meysam Jun 04 '18 at 08:26
6

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.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • 5
    this is caused by the warning `Warning CS1998 This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread` this makes people think that the task wont be async if you don't use the await keyword – MikeT May 02 '18 at 15:29