Код: Выделить всё
internal class Program
{
static async Task Main(string[] args)
{
Console.WriteLine($"Before ParallelForeach on thread: {Environment.CurrentManagedThreadId}"); //Main debug 1
await TaskContainingParallelForeach();
Console.WriteLine($"After ParallelForeach on thread: {Environment.CurrentManagedThreadId}"); //Main debug 2
int counter = 0;
while (counter < 7)
{
Console.WriteLine($"{counter}. one second gone on thread: {Environment.CurrentManagedThreadId} at {DateTimeOffset.Now.ToUnixTimeMilliseconds()}");
Thread.Sleep(1000);
counter++;
}
Console.ReadKey();
}
public static Task TaskContainingParallelForeach()
{
return Task.Run(() =>
{
Console.WriteLine($"Inside ParallelForeach on thread: {Environment.CurrentManagedThreadId}");
int[] numbers = { 1, 3, 2 };
Parallel.ForEach(numbers, (num) =>
{
Console.WriteLine($"{num} is running on thread: {Environment.CurrentManagedThreadId}");
Thread.Sleep(num * 1000);
Console.WriteLine($"{num} stopped on thread: {Environment.CurrentManagedThreadId} at {DateTimeOffset.Now.ToUnixTimeMilliseconds()}");
});
});
}}
Код: Выделить всё
Console Output without await:
Before TaskContainingParallelForeach on thread: 1
Inside TaskContainingParallelForeach on thread: 3 // why is -below- first done all the work from the ParallelForeach?
1 is running on thread: 3
3 is running on thread: 4
2 is running on thread: 6
1 stopped on thread: 3 at 1734365740995
2 stopped on thread: 6 at 1734365741996
3 stopped on thread: 4 at 1734365742996
After TaskContainingParallelForeach on thread: 3 // why is it the thread with id 3 here - expected was 1
0. one second gone on thread: 3 at 1734365742996
1. one second gone on thread: 3 at 1734365743997
2. one second gone on thread: 3 at 1734365745008
3. one second gone on thread: 3 at 1734365746023
4. one second gone on thread: 3 at 1734365747035
5. one second gone on thread: 3 at 1734365748047
6. one second gone on thread: 3 at 1734365749061
И почему идентификатор потока для After TaskContainingParallelForeach в потоке 3, а не 1? И почему цикл while не выполняется в потоке 1?
Однако удаление оператора await из await TaskContainingParallelForeach(); дает желаемый результат:
Код: Выделить всё
Console output without await:
Before TaskContainingParallelForeach on thread: 1
After TaskContainingParallelForeach on thread: 1
Inside TaskContainingParallelForeach on thread: 3
1 is running on thread: 3
3 is running on thread: 4
2 is running on thread: 5
0. one second gone on thread: 1 at 1734364096009
1 stopped on thread: 3 at 1734364097021
1. one second gone on thread: 1 at 1734364097036
2 stopped on thread: 5 at 1734364098027
2. one second gone on thread: 1 at 1734364098043
3 stopped on thread: 4 at 1734364099022
3. one second gone on thread: 1 at 1734364099053
4. one second gone on thread: 1 at 1734364100064
5. one second gone on thread: 1 at 1734364101077
6. one second gone on thread: 1 at 1734364102078
- все работает Цикл Parallel.Foreach запущен
- Тем временем цикл while начал свою работу
- Цикл while находится в ожидаемом потоке 1.
Заранее спасибо!
Подробнее здесь: https://stackoverflow.com/questions/792 ... her-work-i
Мобильная версия