using System;
using System.Threading.Tasks;
namespace pc.tplExample04
{
class Program
{
static Random rnd = new Random(DateTime.Now.Millisecond);
static void Main(string[] args)
{
Console.WriteLine("using named method");
Parallel.For(0, 5, Square);
Console.WriteLine("using anonymous method");
Parallel.For(0, 5, delegate (int num)
{
Task.WaitAll(Task.Delay(rnd.Next(1000,
2000)));
Console.WriteLine($"{num} squared is {num * num}");
});
Console.WriteLine("using lambda
expression");
Parallel.For(0, 5, num =>
{
Task.WaitAll(Task.Delay(rnd.Next(1000,
2000)));
Console.WriteLine($"{num} squared is {num * num}");
});
}
static void Square(int num)
{
Task.WaitAll(Task.Delay(rnd.Next(1000,
2000)));
Console.WriteLine($"{num} squared is {num * num}");
}
}
}
using System;
using System.Threading.Tasks;
namespace pc.tplExample04
{
class Program
{
static Random rnd = new Random(DateTime.Now.Millisecond);
static void Main(string[] args)
{
var nums = new int[10];
for (int i = 0; i
< nums.Length; i++)
nums[i] = rnd.Next(5, 15);
Console.WriteLine("using named method");
Parallel.ForEach(nums, Square);
Console.WriteLine("using anonymous method");
Parallel.ForEach(nums, delegate (int num)
{
Task.WaitAll(Task.Delay(rnd.Next(1000,
2000)));
Console.WriteLine($"{num} squared is {num * num}");
});
Console.WriteLine("using lambda
expression");
Parallel.ForEach(nums, num =>
{
Task.WaitAll(Task.Delay(rnd.Next(1000,
2000)));
Console.WriteLine($"{num} squared is {num * num}");
});
}
static void Square(int num)
{
Task.WaitAll(Task.Delay(rnd.Next(1000,
2000)));
Console.WriteLine($"{num} squared is {num * num}");
}
}
}