Lock是常用的同步锁,但是我们无法在Lock的内部实现异步调用,比如我们无法使用await.
以下面的代码为例,当你在lock内部使用await时,VS会报错提醒。
最简单的解决办法就是使用第三方的库Nito.AsyncEx。可以通过Nuget安装。
通过AsyncLock就可以在锁的内部实现异步操作了。
样例代码如下:
1 public class AsyncLockDemo 2 { 3 //private readonly object _mutex = new object(); 4 private readonly AsyncLock _mutex = new AsyncLock(); 5 private int i = 0; 6 public void Execute() 7 { 8 Console.WriteLine("before call foo1: "+ i); 9 10 Foo1().ContinueWith(new Action<Task>(t => 11 { 12 Console.WriteLine("foo1 completed: " + i); 13 })); 14 15 Console.WriteLine("after call foo1: " + i); 16 17 Console.WriteLine("before call foo2: " + i); 18 19 Foo2().ContinueWith(new Action<Task>(t => 20 { 21 Console.WriteLine("foo2 completed: " + i); 22 })); 23 24 Console.WriteLine("after call foo2: " + i); 25 } 26 27 public async Task Foo1() 28 { 29 using (await _mutex.LockAsync()) 30 { 31 await Task.Delay(TimeSpan.FromSeconds(1)); 32 Console.WriteLine("Foo1 start: " + i); 33 await DoSomethingAsync(1); 34 Console.WriteLine("Foo1 end: " + i); 35 } 36 } 37 38 public async Task Foo2() 39 { 40 using (await _mutex.LockAsync()) 41 { 42 Console.WriteLine("Foo2 start: " + i); 43 //await Task.Delay(TimeSpan.FromSeconds(1)); 44 await DoSomethingAsync(2); 45 Console.WriteLine("Foo2 end: " + i); 46 } 47 } 48 49 private Task DoSomethingAsync(int j) 50 { 51 return Task<string>.Run(() => 52 { 53 Thread.Sleep(2000); 54 i = j; 55 }); 56 } 57 }
运行结果如下: