File tree Expand file tree Collapse file tree 1 file changed +13
-7
lines changed
samples/snippets/csharp/VS_Snippets_CLR_System/system.threading.semaphoreslim/cs Expand file tree Collapse file tree 1 file changed +13
-7
lines changed Original file line number Diff line number Diff line change @@ -25,17 +25,23 @@ public static void Main()
25
25
// Each task begins by requesting the semaphore.
26
26
Console . WriteLine ( "Task {0} begins and waits for the semaphore." ,
27
27
Task . CurrentId ) ;
28
+
29
+ int semaphoreCount ;
28
30
semaphore . Wait ( ) ;
31
+ try
32
+ {
33
+ Interlocked . Add ( ref padding , 100 ) ;
29
34
30
- Interlocked . Add ( ref padding , 100 ) ;
31
-
32
- Console . WriteLine ( "Task {0} enters the semaphore." , Task . CurrentId ) ;
33
-
34
- // The task just sleeps for 1+ seconds.
35
- Thread . Sleep ( 1000 + padding ) ;
35
+ Console . WriteLine ( "Task {0} enters the semaphore." , Task . CurrentId ) ;
36
36
37
+ // The task just sleeps for 1+ seconds.
38
+ Thread . Sleep ( 1000 + padding ) ;
39
+ }
40
+ finally {
41
+ semaphoreCount = semaphore . Release ( ) ;
42
+ }
37
43
Console . WriteLine ( "Task {0} releases the semaphore; previous count: {1}." ,
38
- Task . CurrentId , semaphore . Release ( ) ) ;
44
+ Task . CurrentId , semaphoreCount ) ;
39
45
} ) ;
40
46
}
41
47
You can’t perform that action at this time.
0 commit comments