In this code, when you use await Task.Delay(5000);
, the line Console.WriteLine("START TestSemaPhoreSlims:ID[" + ids + "]...
is executed without any delay.
However, when you use Thread.Sleep(5000);
, you observe a delay in the logs starting from "START TestSemaPhoreSlims:ID[ID:7] workerThreads>>>2040: times>>>16-06-44"
. The expected behavior is that the Console.WriteLine
statement should execute immediately without any delay.
I hope this clarifies the issue. Let me know if you have any further questions.
public class SemaPhoreTest1s
{
public SemaPhoreTest1s()
{
for (int i = 1; i <= 10; i++)
{
int idnums = i;
Task.Run(() => TestSemaPhoreSlims("ID:" + idnums));
}
Console.WriteLine("MAIN THREAD:" + Thread.CurrentThread.ManagedThreadId);
Console.ReadLine();
}
public async void TestSemaPhoreSlims(string ids)
{
int workerThreads, completionPortThreads;
ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
Console.WriteLine("START TestSemaPhoreSlims:ID[" + ids + "] workerThreads>>>" + workerThreads + ": times>>>" + DateTime.Now.ToString("HH-mm-ss"));
Thread.Sleep(5000);
//await Task.Delay(5000);
}
}
MAIN THREAD:1
START TestSemaPhoreSlims:ID\[ID:2\] workerThreads\>\>\>2041: times\>\>\>16-06-43
START TestSemaPhoreSlims:ID\[ID:1\] workerThreads\>\>\>2041: times\>\>\>16-06-43
START TestSemaPhoreSlims:ID\[ID:5\] workerThreads\>\>\>2041: times\>\>\>16-06-43
START TestSemaPhoreSlims:ID\[ID:4\] workerThreads\>\>\>2041: times\>\>\>16-06-43
START TestSemaPhoreSlims:ID\[ID:3\] workerThreads\>\>\>2041: times\>\>\>16-06-43
START TestSemaPhoreSlims:ID\[ID:6\] workerThreads\>\>\>2041: times\>\>\>16-06-43
START TestSemaPhoreSlims:ID\[ID:7\] workerThreads\>\>\>2040: times\>\>\>16-06-44
START TestSemaPhoreSlims:ID\[ID:8\] workerThreads\>\>\>2039: times\>\>\>16-06-45
START TestSemaPhoreSlims:ID\[ID:9\] workerThreads\>\>\>2038: times\>\>\>16-06-46
START TestSemaPhoreSlims:ID\[ID:10\] workerThreads\>\>\>2037: times\>\>\>16-06-47