Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. public class TestController : ApiController
  2. {
  3. // This method must be synchronous
  4. public void Test()
  5. {
  6. Debug.WriteLine($"Main Thread {Thread.CurrentThread.ManagedThreadId}");
  7. // I must use a Task here because otherwise I will have to block async code which will cause a deadlock
  8. var task = Task.Run(() =>
  9. {
  10. Debug.WriteLine($"Child Thread {Thread.CurrentThread.ManagedThreadId}");
  11. SomeAsynchronousWork().GetAwaiter().GetResult();
  12. });
  13. task.GetAwaiter().GetResult();
  14. }
  15.  
  16. public async Task SomeAsynchronousWork()
  17. {
  18. // some work that must be done asynchronously
  19. }
  20. }
  21.  
  22. Main Thread 5
  23. Child Thread 5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement