私は ContinueWith でタスクを連鎖させており、それらすべてを待っています。ループ内に遅延を導入すると (コメント -タスクを完了するのに十分な時間があることを意味します)、すべてのタスクが完了したことがわかりました。遅延を取り除くと、最初のメッセージのみが処理されていることがわかりました。残りのメッセージは処理されません。コードは、すべてのタスクを完了する前にメソッドを終了すると想定しています。私が間違って待っているかどうかはわかりません。
ループ内で遅延を発生させることなく、すべてのタスクを確実に完了するにはどうすればよいですか?
using (var throttler = new SemaphoreSlim(initialConcurrentJobsCount, maxConcurrentJobsCount))
{
for(int counter = 0; counter < msgs.Length; counter++)
{
tasks[counter] = throttler.WaitAsync()
.ContinueWith(r => new VirtualMachineActor().HandleAsync(msgs[counter]))
.ContinueWith(o => throttler.Release());
// When below line is present, all tasks are completed properly.
// When removed only first msg is executed.
await Task.Delay(1000);
}
await Task.WhenAll(tasks);
}