CKTIS / C#
Task.WhenAll: odniesienie praktyczne
Task.WhenAll: odniesienie praktyczne: Task.WhenAll kończy się po wszystkich zadaniach i agreguje wyniki w kolejności wejścia.
Wersja: .NET API; verified against .NET 10 reference surface
Składnia
Task.WhenAll(/* overload-specific arguments */)Task.WhenAll kończy się po wszystkich zadaniach i agreguje wyniki w kolejności wejścia.
Stosuj do niezależnych operacji wykonywanych współbieżnie.
Jeden błąd nie zatrzymuje pozostałych; wynikowy Task kończy się faulted po zakończeniu wszystkich.
Pusta kolekcja daje ukończony Task; null element powoduje ArgumentException.
Kompletny program rzeczywiście używa Task.WhenAll; oczekiwany stdout to dokładnie 3.
Przykłady
using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;
var results = await Task.WhenAll(Task.FromResult(1), Task.FromResult(2));
Console.WriteLine(results.Sum());
Wynik
3
Kompletny program rzeczywiście używa `Task.WhenAll`; oczekiwany stdout to dokładnie `3`.