I'm learning about async programming in C#, and written this code to test Task Parallel Library (Console application):
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
var opr1 = new SlowOperation();
var opr2 = new SlowOperation();
//TASK
Console.WriteLine("Started processing using TASK. Start: {0}", sw.Elapsed);
sw.Start();
Task.Factory.StartNew(() => opr1.PerformSlowOperation(1));
Task.Factory.StartNew(() => opr2.PerformSlowOperation(2));
Console.WriteLine("Stopped processing using TASK. Stop: {0}", sw.Elapsed);
sw.Stop();
}
where slow operation is:
public class SlowOperation
{
public void PerformSlowOperation(int id)
{
var rand = new Random();
double sum = 0;
for (int i = 0; i < 100000000; i++)
{
var number = Convert.ToDouble(rand.Next(100)) / 100;
sum += number;
}
Console.WriteLine("Finished processing operation no. {0}. Final sum calculated is: {1}", id, sum.ToString("0.##"));
}
}
Could anyone please help me to understand why sum produced by each instance of SlowOperation class is exactly the same?