Znalostní bázeCentrum zkoušek
CSČeština
Vybrat jazyk13

CKTIS / C#

Task.WaitAsync: praktická reference

Task.WaitAsync: praktická reference: Task.WaitAsync přidá k čekání timeout nebo zrušení bez změny původního Task.

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

Syntaxe

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

Task.WaitAsync přidá k čekání timeout nebo zrušení bez změny původního Task.

Použijte k omezení čekání na operaci, kterou neovládáte.

Timeout nezastaví podkladovou operaci; její pozdější výsledek nebo chybu musíte řešit zvlášť.

Již dokončený Task může být vrácen bez alokace; timeout vyhodí TimeoutException.

Úplný program skutečně používá Task.WaitAsync; přesně očekávaný stdout je 12.

Pří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 task = Task.FromResult(12);
Console.WriteLine(await task.WaitAsync(TimeSpan.FromSeconds(1)));

Výsledek

12

Úplný program skutečně používá `Task.WaitAsync`; přesně očekávaný stdout je `12`.

Zdroje