Znalostná bázaCentrum skúšok
SKSlovenčina
Vybrať jazyk13

CKTIS / C#

Task.WhenAll: praktická referencia

Task.WhenAll: praktická referencia: Task.WhenAll sa dokončí po všetkých úlohách a zhromaždí výsledky v poradí vstupu.

Verzia: .NET API; verified against .NET 10 reference surface

Syntax

Task.WhenAll(/* overload-specific arguments */)

Task.WhenAll sa dokončí po všetkých úlohách a zhromaždí výsledky v poradí vstupu.

Použite na nezávislé operácie, ktoré majú bežať súbežne.

Jedna chyba nezastaví ostatné; výsledný Task bude faulted až po dokončení všetkých.

Prázdna kolekcia dá dokončený Task; prvok null spôsobí ArgumentException.

Úplný program skutočne používa Task.WhenAll; presne očakávaný stdout je 3.

Príklady

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());

Výsledok

3

Úplný program skutočne používa `Task.WhenAll`; presne očakávaný stdout je `3`.

Zdroje