async-await works with sequel. To be effective and reduce planning, these continuations are usually executed in the same thread that completed the previous task.
This means that your special thread not only performs tasks, but also performs all the continuations after these tasks (the for loop itself). This can be seen by typing the stream identifier:
using (var sa = new SampleActor()) { for (int i = 0; i < 3; i++) { Console.WriteLine(await sa.GetThreadStatusAsync()); Console.WriteLine("Continue on thread :" + Thread.CurrentThread.ManagedThreadId); } }
When the for loop completes and the SampleActor is located, you call Thread.Join from the same thread that you are trying to join so you get stuck. Your situation is as follows:
public static void Main() { Thread thread = null; thread = new Thread(() => { Thread.Sleep(100); thread.Join(); Console.WriteLine("joined"); }); thread.Start(); }
In .Net 4.6 you can solve this with TaskCreationOptions.RunContinuationsAsynchronously , but in the current version you can specify the default value of TaskScheduler :
public Task<string> GetThreadStatusAsync() { var task = new Task<string>(GetThreadStatus); QueueTask(task); return task.ContinueWith(task1 => task1.GetAwaiter().GetResult(), TaskScheduler.Default); }
i3arnon
source share