I\'m using Threading timer to do some periodic job:
private static async void TimerCallback(object state)
{
if (Interlocked.CompareExchange(ref curre
This:
var tasksRead = Enumerable.Range(3, 35).Select(i => ReadSensorsAsync(i));
creates a lazily evaluated IEnumerable which maps numbers to method invocation results. ReadSensorsAsync
is not invoked here, it will be invoked during evaluation.
This IEnumerable is evaluated twice. Here:
await Task.WhenAll(tasksRead);
and here:
// Here, another lazy IEnumerable is created based on tasksRead.
var tasksRecord = tasksRead.Where(...).Select(...);
await Task.WhenAll(tasksRecord); // Here, it is evaluated.
Thus, ReadSensorsAsync
is invoked twice.
As csharpfolk suggested in the comments, materializing the IEnumerable should fix this:
var tasksRead = Enumerable.Range(3, 35).Select(i => ReadSensorsAsync(i)).ToList();