Why async functions are called twice?

后端 未结 3 690
悲哀的现实
悲哀的现实 2021-01-19 00:29

I\'m using Threading timer to do some periodic job:

private static async void TimerCallback(object state)
{
        if (Interlocked.CompareExchange(ref curre         


        
3条回答
  •  悲哀的现实
    2021-01-19 01:04

    I think I know WHY ! In two words: - reason that function with await impliciti create a callback thread. Better you can see how explain it Jeffrey Richter on this video https://wintellectnow.com/Videos/Watch?videoId=performing-i-o-bound-asynchronous-operations from 00:17:25

    just try it:

    var tasksRead = Enumerable.Range(3, 35).Select(i => ReadSensorsAsync(i));
    var tasksRecord = tasksRead.Where(x => x.Result != null).Select(x => RecordReadingAsync(x.Result));
    
    await Task.WhenAll(tasksRead);
    
    await Task.WhenAll(tasksRecord);
    

提交回复
热议问题