static void Main(string[] args) { //# Note that console applications don’t cause this deadlock. //# They have a thread pool SynchronizationContext instead of a one-chunk-at-a-time SynchronizationContext, //# so when the await completes, it schedules the remainder of the async method on a thread pool thread. var deadlock = new DeadlockSample(); deadlock.RunDeadlock(); Console.WriteLine("Press any key to exit."); Console.ReadKey(); }
private void button2_Click(object sender, EventArgs e) { //# 1.비동기 메서드를 Block(Wait)해서 발생하는 Deadlock 예제 //# When the await completes, it attempts to execute the remainder of the async method within the captured context. //# But that context already has a thread in it, which is (synchronously) waiting for the async method to complete. //# They’re each waiting for the other, causing a deadlock. //# UI가 없는 ConsoleApp에서는 위 코드에서 Deaklock이 발생하지 않음 //# ConsoleApp1 프로젝트 참조 var deadlock = new DeadlockSample(); deadlock.RunDeadlock(); //# 2.해결책 //# - 시그니쳐를 async로 변경 필요 //var deadlock = new DeadlockSample(); //await deadlock.RunDeadlockAsync(); }