/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/standard/parallel-programming/snippets/cs/attached1.cs
38 строк
1 KB
David Pine
Address concerns about chaining tasks and lacking continuation examples with .Unwrap() (#19611)
28 июл 2020, 15:00
Не верифицирован
28 июл 2020, 15:00
9685f88
Код
Авторство
О чём код?
using System; using System.Threading.Tasks; public class AttachedExample { public static async Task Main() { await Task.Factory .StartNew( () => { Console.WriteLine($"Running antecedent task {Task.CurrentId}..."); Console.WriteLine("Launching attached child tasks..."); for (int ctr = 1; ctr <= 5; ctr++) { int index = ctr; Task.Factory.StartNew(async value => { Console.WriteLine($" Attached child task #{value} running"); await Task.Delay(1000); }, index, TaskCreationOptions.AttachedToParent); } Console.WriteLine("Finished launching attached child tasks..."); }).ContinueWith( antecedent => Console.WriteLine($"Executing continuation of Task {antecedent.Id}")); } } // The example displays the similar output: // Running antecedent task 1... // Launching attached child tasks... // Finished launching attached child tasks... // Attached child task #1 running // Attached child task #5 running // Attached child task #3 running // Attached child task #2 running // Attached child task #4 running // Executing continuation of Task 1