Stream blocked after waiting

With this code:

    static void Main(string[] args)
    {
        Console.WriteLine("Main Thread Pre - " + GetNativeThreadId(System.Threading.Thread.CurrentThread));
        Task.Run(() => AsyncMethod()).Wait();
        Console.WriteLine("Main Thread Post - " + GetNativeThreadId(System.Threading.Thread.CurrentThread));
        Console.ReadKey();
    }

    static async Task AsyncMethod()
    {
        Console.WriteLine("AsyncMethod Thread Pre - " + GetNativeThreadId(System.Threading.Thread.CurrentThread));
        await Task.Delay(4000).ConfigureAwait(false);
        Console.WriteLine("AsyncMethod Thread Post - " + GetNativeThreadId(System.Threading.Thread.CurrentThread));
    }

Conclusion:

Main Thread Pre - 8652
AsyncMethod Thread Pre - 4764
AsyncMethod Thread Post - 1768
Main Thread Post - 8652

Using Concurrency, I see that within a 4 second delay, the 4764 thread is stuck in Sync. In the end, it disconnects from the main thread when shutting down.

Should stream 4764 be returned ThreadPoolwhen it falls in await? (However, I do not know how this will look inside the Concurrency Visualizer)

+4
source share
1 answer

Should I return 4764 thread to ThreadPool after waiting?

Yes. And so it is.

(However, I do not know how this will look inside the Concurrency Visualizer)

. , , .

:

ThreadPool.QueueUserWorkItem(o =>
    {
        Console.WriteLine("worker: " + GetNativeThreadId(System.Threading.Thread.CurrentThread));
        Thread.Sleep(250);
    });

( , , - :)).

, , , .:)


, , Task. , Synchronization, .

. , ? , , . , , . . .

- (, Task) , , , - . , , , - .

, , WaitForSingleObject(), , ReleaseSemaphore().

Synchronization , .

+4

Source: https://habr.com/ru/post/1584073/


All Articles