CKTIS / C#
Task.WhenAll: praktična referenca
Task.WhenAll: praktična referenca: Task.WhenAll se konča po vseh opravilih in zbere rezultate v vrstnem redu vhoda.
Različica: .NET API; verified against .NET 10 reference surface
Sintaksa
Task.WhenAll(/* overload-specific arguments */)Task.WhenAll se konča po vseh opravilih in zbere rezultate v vrstnem redu vhoda.
Uporabite za neodvisne operacije, ki naj tečejo sočasno.
Ena napaka ne ustavi drugih; nastali Task postane faulted šele po zaključku vseh.
Prazna zbirka da zaključen Task; element null povzroči ArgumentException.
Celoten program dejansko uporabi Task.WhenAll; natančno pričakovani stdout je 3.
Primeri
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
Celoten program dejansko uporabi `Task.WhenAll`; natančno pričakovani stdout je `3`.