I am trying to unravel the asynchronous wait function in C #. I wrote the code below to perform several tasks asynchronously - currently all they do is raise an event after a certain time.
public class Program
{
public static Stopwatch Watch = new Stopwatch();
public static void Main(string[] args)
{
AsyncClass asyncClass = new AsyncClass();
asyncClass.WaitSecondsAsyncCompleted += asyncClass_WaitSecondsAsyncCompleted;
List<Task> tasks = new List<Task>();
Watch.Start();
for (int i = 1; i < 6; i++)
{
tasks.Add(asyncClass.WaitSecondsAsync(i, Watch));
}
Task.WaitAll(tasks.ToArray());
Console.ReadLine();
}
private static void asyncClass_WaitSecondsAsyncCompleted(int i)
{
Console.WriteLine("{1} : Async Method Called: waited for {0} seconds", i, Watch.ElapsedMilliseconds);
}
}
public class AsyncClass
{
public event Action<int> WaitSecondsAsyncCompleted;
public async Task WaitSecondsAsync(int x, Stopwatch watch)
{
await Task.Run(() =>
{
Thread.Sleep(x * 500);
});
if (WaitSecondsAsyncCompleted != null)
{
WaitSecondsAsyncCompleted(x);
}
}
}
I expected the task to be completed approximately once every half a second, but this is not quite what I see. Instead, the first four tasks are completed on time, but the final task has an additional half second delay:

This seems very strange - and the only thing I can think of is that there is a limit on the number of threads available for the task, and that this restriction is very small, and therefore the fifth task must wait to complete the first task before it starts .
, , - , , . (, 10 , 5 , 8 ). .
:
- - , ?
- , ?
- , , , , - , . - , ?

, , ( TPL?), , , 5 . , default threadPool , .