回答編集履歴
1
追記
test
CHANGED
@@ -47,3 +47,61 @@
|
|
47
47
|
}
|
48
48
|
|
49
49
|
```
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
---
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
> BクラスにSemaphoreSlimなり使う方法
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
```C#
|
62
|
+
|
63
|
+
using System;
|
64
|
+
|
65
|
+
using System.Threading.Tasks;
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
class A {
|
70
|
+
|
71
|
+
static void Main () {
|
72
|
+
|
73
|
+
B b = new B ();
|
74
|
+
|
75
|
+
b.Print (1);
|
76
|
+
|
77
|
+
b.Print (2);
|
78
|
+
|
79
|
+
Console.ReadLine ();
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
class B {
|
88
|
+
|
89
|
+
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
|
90
|
+
|
91
|
+
public async Task Print (int i) {
|
92
|
+
|
93
|
+
await _semaphore.WaitAsync();
|
94
|
+
|
95
|
+
Console.WriteLine (i + " start");
|
96
|
+
|
97
|
+
await Task.Delay (4000);
|
98
|
+
|
99
|
+
Console.WriteLine (i + " end");
|
100
|
+
|
101
|
+
_semaphore.Release();
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
```
|