What is the difference between these two functions using async / wait / TPL?

I think I successfully embarrassed myself during the day.

public void DoSomething1()
{
    Task.Delay(1000);
}

public async void DoSomething2()
{
    await Task.Delay(1000);
}

What is the difference between these two functions in terms of what happens inside them when they are called? What is the purpose of using a method asyncthat does not return Task?

+3
source share
1 answer

What is the difference between these two functions in terms of what happens inside them when called?

DoSomething1is a synchronous method. In this way:

  • It starts the asynchronous delay and then ignores it.
  • Any exceptions to the asynchronous delay are silently ignored.
  • DoSomething .

DoSomething2 - void. :

  • .
  • SynchronizationContext, DoSomething2. .
  • DoSomething2 SynchronizationContext .

async, ?

async void . , F #. async void #/VB, .

, async void ( , ICommand.Execute MVVM).

+6

All Articles