使用Nito.AsyncEx實現異步鎖

Lock是經常使用的同步鎖,可是咱們沒法在Lock的內部實現異步調用,好比咱們沒法使用await.異步

如下面的代碼爲例,當你在lock內部使用await時,VS會報錯提醒。async

最簡單的解決辦法就是使用第三方的庫Nito.AsyncEx。能夠經過Nuget安裝。spa

經過AsyncLock就能夠在鎖的內部實現異步操做了。3d

樣例代碼以下:code

 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     }

運行結果以下:blog

相關文章
相關標籤/搜索