Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- using System.Threading.Tasks;
- namespace MonitorAwait
- {
- class Program
- {
- static object lockObject = new object();
- static async Task Main(string[] args)
- {
- Monitor.Enter(lockObject);
- Console.WriteLine("acquired lock1");
- var value = await GetValue();
- Console.WriteLine("after await");
- Monitor.Enter(lockObject);
- Console.WriteLine("acquired lock2");
- Monitor.Exit(lockObject);
- Console.WriteLine("released lock1");
- Monitor.Exit(lockObject);
- }
- private static async Task<int> GetValue()
- {
- await Task.Delay(10);
- return await Task.FromResult(10);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement