Skip to content

Commit 65eca8c

Browse files
authored
Format the sample code to make it easier to read (#4208)
The way of how Task.Run avoided indenting it's content and hiding the closing braces on the last Console.WriteLine line made it really hard to see what's going on here.
1 parent b4ec0e4 commit 65eca8c

File tree

1 file changed

+16
-14
lines changed
  • samples/snippets/csharp/VS_Snippets_CLR_System/system.threading.semaphoreslim/cs

1 file changed

+16
-14
lines changed

samples/snippets/csharp/VS_Snippets_CLR_System/system.threading.semaphoreslim/cs/example.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,27 @@ public static void Main()
1616
Console.WriteLine("{0} tasks can enter the semaphore.",
1717
semaphore.CurrentCount);
1818
Task[] tasks = new Task[5];
19-
19+
2020
// Create and start five numbered tasks.
21-
for(int i = 0; i <= 4; i++)
21+
for (int i = 0; i <= 4; i++)
2222
{
23-
tasks[i] = Task.Run( () => {
24-
// Each task begins by requesting the semaphore.
25-
Console.WriteLine("Task {0} begins and waits for the semaphore.",
26-
Task.CurrentId);
27-
semaphore.Wait();
23+
tasks[i] = Task.Run(() =>
24+
{
25+
// Each task begins by requesting the semaphore.
26+
Console.WriteLine("Task {0} begins and waits for the semaphore.",
27+
Task.CurrentId);
28+
semaphore.Wait();
2829

29-
Interlocked.Add(ref padding, 100);
30+
Interlocked.Add(ref padding, 100);
3031

31-
Console.WriteLine("Task {0} enters the semaphore.", Task.CurrentId);
32+
Console.WriteLine("Task {0} enters the semaphore.", Task.CurrentId);
3233

33-
// The task just sleeps for 1+ seconds.
34-
Thread.Sleep(1000 + padding);
34+
// The task just sleeps for 1+ seconds.
35+
Thread.Sleep(1000 + padding);
3536

36-
Console.WriteLine("Task {0} releases the semaphore; previous count: {1}.",
37-
Task.CurrentId, semaphore.Release()); } );
37+
Console.WriteLine("Task {0} releases the semaphore; previous count: {1}.",
38+
Task.CurrentId, semaphore.Release());
39+
});
3840
}
3941

4042
// Wait for half a second, to allow all the tasks to start and block.
@@ -47,7 +49,7 @@ public static void Main()
4749
semaphore.CurrentCount);
4850
// Main thread waits for the tasks to complete.
4951
Task.WaitAll(tasks);
50-
52+
5153
Console.WriteLine("Main thread exits.");
5254
}
5355
}

0 commit comments

Comments
 (0)