39
1
using System;
2
using System.Linq;
3
using System.Threading.Tasks;
4
5
public class Program
6
{
7
public static void Main()
8
{
9
var numbers = Enumerable.Range(0, 10);
10
Parallel.ForEach(numbers, i =>
11
{
12
VoteMachine.RegisterVote();
13
});
14
Console.WriteLine(VoteMachine.TotalVotes);
15
}
16
}
17
18
public class VoteMachine
19
{
20
private static int _totalVotes = 0;
21
22
static VoteMachine()
23
{
24
}
25
26
public static void RegisterVote()
27
{
28
_totalVotes += 1;
29
Console.WriteLine("Registered Vote #" + _totalVotes);
30
}
31
32
public static int TotalVotes
33
{
34
get
35
{
36
return _totalVotes;
37
}
38
}
39
}
Cached Result
LINQ Tutorials
.NET Threading Tutorials
.NET Threading Tutorials