CKTIS / C#
Task.WhenAll: praktična referenca
Task.WhenAll: praktična referenca: Task.WhenAll završava nakon svih zadataka i prikuplja rezultate redoslijedom ulaza.
Verzija: .NET API; verified against .NET 10 reference surface
Sintaksa
Task.WhenAll(/* overload-specific arguments */)Task.WhenAll završava nakon svih zadataka i prikuplja rezultate redoslijedom ulaza.
Koristite za neovisne operacije koje trebaju teći istodobno.
Jedna pogreška ne zaustavlja ostale; rezultatni Task postaje faulted tek nakon dovršetka svih.
Prazna kolekcija daje dovršen Task; element null uzrokuje ArgumentException.
Cijeli program doista koristi Task.WhenAll; točno očekivani stdout jest 3.
Primjeri
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());
Rezultat
3
Cijeli program doista koristi `Task.WhenAll`; točno očekivani stdout jest `3`.