As I understood from the answers on the forum, it is better not to use asynchronous methods with Parallel.For/ForEach/Invoke. I noticed that there is no waiting on the main thread and the program just terminates.
Do I understand correctly that the Parallel.For/ForEach/Invoke methods do not wait on the main thread, how does it happen with synchronous methods and is it better not to use asynchronous methods with them?
Parallel.For. No waiting on the main thread
Random rnd = new Random();
async Task MyMethod()
{
while (true)
{
Console.WriteLine(rnd.Next(1, 101));
await Task.Delay(1000);
}
}
Parallel.For(1, 5, (i) => MyMethod());
Parallel.ForEach. No waiting on the main thread
Random rnd = new Random();
async Task MyMethod()
{
while (true)
{
Console.WriteLine(rnd.Next(1, 101));
await Task.Delay(1000);
}
}
int[] MyArray = new int[5];
Parallel.ForEach(MyArray, (i) => MyMethod());
Parallel.Invoke. No waiting on the main thread
Random rnd = new Random();
async Task MyMethod()
{
while (true)
{
Console.WriteLine(rnd.Next(1, 101));
await Task.Delay(1000);
}
}
Parallel.Invoke(() => MyMethod(), () => MyMethod(), () => MyMethod());