daily pastebin goal
9%
SHARE
TWEET

Untitled

a guest Mar 22nd, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top