Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4.  
  5. namespace MonitorAwait
  6. {
  7. class Program
  8. {
  9. static object lockObject = new object();
  10.  
  11. static async Task Main(string[] args)
  12. {
  13. Monitor.Enter(lockObject);
  14. Console.WriteLine("acquired lock1");
  15. var value = await GetValue();
  16. Console.WriteLine("after await");
  17. Monitor.Enter(lockObject);
  18. Console.WriteLine("acquired lock2");
  19. Monitor.Exit(lockObject);
  20. Console.WriteLine("released lock1");
  21. Monitor.Exit(lockObject);
  22. }
  23.  
  24. private static async Task<int> GetValue()
  25. {
  26. await Task.Delay(10);
  27. return await Task.FromResult(10);
  28. }
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement