1

Let say that we have a method:

public async Task FlushBuffer(List<UmtsCellKpiReceived> kpis)
{
    await _umtsCellService.ProcessUmtsCellKpi(kpis).ConfigureAwait(false);
}

Does it make sense to await this Task here? In one of the recent discussions I had, I learned that not awaiting creates an orphaned task which risks an UnobservedTaskException if MyTask() throws.

Is this a valid reason to await everything ?

Cemre Mengü
  • 18,062
  • 27
  • 111
  • 169
  • 1
    No as long as you return the task directly. The only reason to use `await` is because you want to do something with the results. If you don't just return the task to the caller. – Panagiotis Kanavos Mar 21 '17 at 14:26
  • @PanagiotisKanavos is right. I didn't properly read your code. – spender Mar 21 '17 at 14:28
  • 1
    @Cemre, please go to this link http://www.albahari.com/threading/part5.aspx#_TaskCreationOptions and you will see your answer on this topic Exception-Handling Tasks. Hope this helps!!! – Zinov Mar 21 '17 at 14:37

1 Answers1

2

In your case, just a single line method, you can just return the Task itself, no async/await required. The calling code is able to await the Task itself if it makes sense for it to do so.

FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
  • So in that situation, does the method even need to be marked `async`? I would think not since the purpose of the `async` keyword is essentially to enable the use of the `await` keyword. – Chris Dunaway Mar 21 '17 at 21:14
  • You're correct. You wouldn't mark the method async, and in fact, an async method without any await statements will produce a warning. – FishBasketGordo Mar 21 '17 at 21:15