# Runtime\_CompilerServices\_TaskAwaiter

***

**1. Waiting for a Task to Complete:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Wait for the task to complete.
int result = await task;
```

**2. Waiting for a Task with a Timeout:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Wait for the task to complete with a timeout of 10 seconds.
int result = await task.ConfigureAwait(false) with { Timeout = TimeSpan.FromSeconds(10) };
```

**3. Waiting for Multiple Tasks to Complete:**

```csharp
// Create multiple tasks.
Task<int>[] tasks = { Task.Run(() => CalculateResult1()), Task.Run(() => CalculateResult2()) };

// Wait for all tasks to complete.
int[] results = await Task.WhenAll(tasks);
```

**4. Waiting for any of Multiple Tasks to Complete:**

```csharp
// Create multiple tasks.
Task<int>[] tasks = { Task.Run(() => CalculateResult1()), Task.Run(() => CalculateResult2()) };

// Wait for any of the tasks to complete.
int result = await Task.WhenAny(tasks);
```

**5. Waiting for a Task while Handling Exceptions:**

```csharp
try
{
    // Create a task.
    Task<int> task = Task.Run(() => CalculateResult());

    // Wait for the task to complete.
    int result = await task;
}
catch (Exception ex)
{
    // Handle the exception.
}
```

**6. Waiting for a Task and Returning a Default Value:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Wait for the task to complete, or return a default value if it fails.
int result = await task.ConfigureAwait(false) ?? 0;
```

**7. Waiting for a Task with Cancellation:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a cancellation token source.
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();

// Wait for the task to complete or be cancelled.
int result = await task.ConfigureAwait(false) with { CancellationToken = cancellationTokenSource.Token };
```

**8. Waiting for a Task with Progress:**

```csharp
// Create a task that reports progress.
Task<int> task = Task.Run(() => CalculateResult(), new Progress<int>(ProgressReport));

// Wait for the task to complete.
int result = await task;

void ProgressReport(int progress)
{
    // Handle progress updates.
}
```

**9. Waiting for a Task with Unwrapped Result:**

```csharp
// Create a task.
Task<Task<int>> nestedTask = Task.Run(() => Task.FromResult(CalculateResult()));

// Wait for the nested task to complete and unwrap the result.
int result = await nestedTask.Unwrap();
```

**10. Waiting for a Task with Result and Exception Information:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Wait for the task to complete and get the result and exception information.
ValueTask<(int Result, Exception Exception)> resultWithException = await task.WithAwaiter(result: true, exception: true);
```

**11. Waiting for a Task with a Custom Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a custom awaiter.
CustomAwaiter<int> customAwaiter = new CustomAwaiter<int>(task);

// Wait for the task to complete using the custom awaiter.
int result = await customAwaiter;
```

**12. Waiting for a Task with an Unsafe Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create an unsafe awaiter.
UnsafeAwaiter<int> unsafeAwaiter = new UnsafeAwaiter<int>(task);

// Wait for the task to complete using the unsafe awaiter.
int result = await unsafeAwaiter;
```

**13. Waiting for a Task with a Cached Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a cached awaiter.
CachedAwaiter<int> cachedAwaiter = new CachedAwaiter<int>(task);

// Wait for the task to complete using the cached awaiter.
int result = await cachedAwaiter;
```

**14. Waiting for a Task with a Remoting Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a remoting awaiter.
RemotingAwaiter<int> remotingAwaiter = new RemotingAwaiter<int>(task);

// Wait for the task to complete using the remoting awaiter.
int result = await remotingAwaiter;
```

**15. Waiting for a Task with a Synchronization Context Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a synchronization context awaiter.
SynchronizationContextAwaiter<int> synchronizationContextAwaiter = new SynchronizationContextAwaiter<int>(task);

// Wait for the task to complete using the synchronization context awaiter.
int result = await synchronizationContextAwaiter;
```

**16. Waiting for a Task with a Thread Synchronization Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a thread synchronization awaiter.
ThreadSynchronizationAwaiter<int> threadSynchronizationAwaiter = new ThreadSynchronizationAwaiter<int>(task);

// Wait for the task to complete using the thread synchronization awaiter.
int result = await threadSynchronizationAwaiter;
```

**17. Waiting for a Task with a Yield Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a yield awaiter.
YieldAwaiter<int> yieldAwaiter = new YieldAwaiter<int>(task);

// Wait for the task to complete using the yield awaiter.
int result = await yieldAwaiter;
```

**18. Waiting for a Task with a Compiler Generated Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Use the compiler-generated awaiter.
int result = await task;
```

**19. Waiting for a Task with a Debugger Display Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a debugger display awaiter.
DebuggerDisplayAwaiter<int> debuggerDisplayAwaiter = new DebuggerDisplayAwaiter<int>(task);

// Wait for the task to complete using the debugger display awaiter.
int result = await debuggerDisplayAwaiter;
```

**20. Waiting for a Task with a Continuation Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a continuation awaiter.
ContinuationAwaiter<int> continuationAwaiter = new ContinuationAwaiter<int>(task);

// Wait for the task to complete using the continuation awaiter.
int result = await continuationAwaiter;
```

**21. Waiting for a Task with a Stateful Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a stateful awaiter.
StatefulAwaiter<int> statefulAwaiter = new StatefulAwaiter<int>(task);

// Wait for the task to complete using the stateful awaiter.
int result = await statefulAwaiter;
```

**22. Waiting for a Task with a Task-Like Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a task-like awaiter.
TaskLikeAwaiter<int> taskLikeAwaiter = new TaskLikeAwaiter<int>(task);

// Wait for the task to complete using the task-like awaiter.
int result = await taskLikeAwaiter;
```

**23. Waiting for a Task with a Null Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a null awaiter.
NullAwaiter<int> nullAwaiter = new NullAwaiter<int>(task);

// Wait for the task to complete using the null awaiter.
int result = await nullAwaiter;
```

**24. Waiting for a Task with a Single-Value Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a single-value awaiter.
SingleValueAwaiter<int> singleValueAwaiter = new SingleValueAwaiter<int>(task);

// Wait for the task to complete using the single-value awaiter.
int result = await singleValueAwaiter;
```

**25. Waiting for a Task with a Multiple-Value Awaiter:**

```csharp
// Create a task.
Task<int[]> task = Task.Run(() => CalculateResult());

// Create a multiple-value awaiter.
MultipleValueAwaiter<int[]> multipleValueAwaiter = new MultipleValueAwaiter<int[]>(task);

// Wait for the task to complete using the multiple-value awaiter.
int[] result = await multipleValueAwaiter;
```

**26. Waiting for a Task with a ValueTuple Awaiter:**

```csharp
// Create a task.
Task<ValueTuple<int, string>> task = Task.Run(() => CalculateResult());

// Create a value tuple awaiter.
ValueTupleAwaiter<int, string> valueTupleAwaiter = new ValueTupleAwaiter<int, string>(task);

// Wait for the task to complete using the value tuple awaiter.
(int result1, string result2) = await valueTupleAwaiter;
```

**27. Waiting for a Task with a Named Value Awaiter:**

```csharp
// Create a task.
Task<(int Result1, string Result2)> task = Task.Run(() => CalculateResult());

// Create a named value awaiter.
NamedValueAwaiter<int, string> namedValueAwaiter = new NamedValueAwaiter<int, string>(task);

// Wait for the task to complete using the named value awaiter.
(int result1, string result2) = await namedValueAwaiter;
```

**28. Waiting for a Task with a Conditional Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a conditional awaiter.
ConditionalAwaiter<int> conditionalAwaiter = new ConditionalAwaiter<int>(task);

// Wait for the task to complete using the conditional awaiter.
int result = await conditionalAwaiter;
```

**29. Waiting for a Task with an Async Iterator Awaiter:**

```csharp
// Create a task.
Task<IAsyncEnumerable<int>> task = Task.Run(() => CalculateResult());

// Create an async iterator awaiter.
AsyncIteratorAwaiter<int> asyncIteratorAwaiter = new AsyncIteratorAwaiter<int>(task);

// Wait for the task to complete using the async iterator awaiter.
await foreach (int result in asyncIteratorAwaiter)
{
    // Process the result.
}
```

**30. Waiting for a Task with a Disposable Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a disposable awaiter.
DisposableAwaiter<int> disposableAwaiter = new DisposableAwaiter<int>(task);

// Wait for the task to complete using the disposable awaiter.
int result = await disposableAwaiter;

// Dispose the awaiter.
disposableAwaiter.Dispose();
```

**31. Waiting for a Task with a CancellationToken Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult(), new CancellationToken());

// Create a cancellation token awaiter.
CancellationTokenAwaiter<int> cancellationTokenAwaiter = new CancellationTokenAwaiter<int>(task);

// Wait for the task to complete using the cancellation token awaiter.
int result = await cancellationTokenAwaiter;
```

**32. Waiting for a Task with a CancellationToken and Timeout Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult(), new CancellationToken(), TimeSpan.FromSeconds(10));

// Create a cancellation token and timeout awaiter.
CancellationTokenAndTimeoutAwaiter<int> cancellationTokenAndTimeoutAwaiter = new CancellationTokenAndTimeoutAwaiter<int>(task);

// Wait for the task to complete using the cancellation token and timeout awaiter.
int result = await cancellationTokenAndTimeoutAwaiter;
```

**33. Waiting for a Task with a Timeout and CancellationToken Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult(), new CancellationToken(), TimeSpan.FromSeconds(10));

// Create a timeout and cancellation token awaiter.
TimeoutAndCancellationTokenAwaiter<int> timeoutAndCancellationTokenAwaiter = new TimeoutAndCancellationTokenAwaiter<int>(task);

// Wait for the task to complete using the timeout and cancellation token awaiter.
int result = await timeoutAndCancellationTokenAwaiter;
```

**34. Waiting for a Task with a TaskSource Awaiter:**

```csharp
// Create a task.
TaskCompletionSource<int> taskSource = new TaskCompletionSource<int>();

// Create a task source awaiter.
TaskSourceAwaiter<int> taskSourceAwaiter = new TaskSourceAwaiter<int>(taskSource);

// Wait for the task to complete using the task source awaiter.
int result = await taskSourceAwaiter;
```

**35. Waiting for a Task with a ManualResetEvent Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a manual reset event awaiter.
ManualResetEventAwaiter<int> manualResetEventAwaiter = new ManualResetEventAwaiter<int>(task);

// Wait for the task to complete using the manual reset event awaiter.
int result = await manualResetEventAwaiter;
```

**36. Waiting for a Task with a WaitHandle Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a wait handle awaiter.
WaitHandleAwaiter<int> waitHandleAwaiter = new WaitHandleAwaiter<int>(task);

// Wait for the task to complete using the wait handle awaiter.
int result = await waitHandleAwaiter;
```

**37. Waiting for a Task with a Semaphore Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a semaphore awaiter.
SemaphoreAwaiter<int> semaphoreAwaiter = new SemaphoreAwaiter<int>(task);

// Wait for the task to complete using the semaphore awaiter.
int result = await semaphoreAwaiter;
```

**38. Waiting for a Task with a Timer Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a timer awaiter.
TimerAwaiter<int> timerAwaiter = new TimerAwaiter<int>(task);

// Wait for the task to complete using the timer awaiter.
int result = await timerAwaiter;
```

**39. Waiting for a Task with a Stopwatch Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a stopwatch awaiter.
StopwatchAwaiter<int> stopwatchAwaiter = new StopwatchAwaiter<int>(task);

// Wait for the task to complete using the stopwatch awaiter.
int result = await stopwatchAwaiter;
```

**40. Waiting for a Task with a TcpClient Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a tcp client awaiter.
TcpClientAwaiter<int> tcpClientAwaiter = new TcpClientAwaiter<int>(task);

// Wait for the task to complete using the tcp client awaiter.
int result = await tcpClientAwaiter;
```

**41. Waiting for a Task with a Socket Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a socket awaiter.
SocketAwaiter<int> socketAwaiter = new SocketAwaiter<int>(task);

// Wait for the task to complete using the socket awaiter.
int result = await socketAwaiter;
```

**42. Waiting for a Task with a FileStream Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a file stream awaiter.
FileStreamAwaiter<int> fileStreamAwaiter = new FileStreamAwaiter<int>(task);

// Wait for the task to complete using the file stream awaiter.
int result = await fileStreamAwaiter;
```

**43. Waiting for a Task with a StreamReader Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a stream reader awaiter.
StreamReaderAwaiter<int> streamReaderAwaiter = new StreamReaderAwaiter<int>(task);

// Wait for the task to complete using the stream reader awaiter.
int result = await streamReaderAwaiter;
```

**44. Waiting for a Task with a StreamWriter Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a stream writer awaiter.
StreamWriterAwaiter<int> streamWriterAwaiter = new StreamWriterAwaiter<int>(task);

// Wait for the task to complete using the stream writer awaiter.
int result = await streamWriterAwaiter;
```

**45. Waiting for a Task with a BinaryReader Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a binary reader awaiter.
BinaryReaderAwaiter<int> binaryReaderAwaiter = new BinaryReaderAwaiter<int>(task);

// Wait for the task to complete using the binary reader awaiter.
int result = await binaryReaderAwaiter;
```

**46. Waiting for a Task with a BinaryWriter Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a binary writer awaiter.
BinaryWriterAwaiter<int> binaryWriterAwaiter = new BinaryWriterAwaiter<int>(task);

// Wait for the task to complete using the binary writer awaiter.
int result = await binaryWriterAwaiter;
```

**47. Waiting for a Task with a XmlReader Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create an xml reader awaiter.
XmlReaderAwaiter<int> xmlReaderAwaiter = new XmlReaderAwaiter<int>(task);

// Wait for the task to complete using the xml reader awaiter.
int result = await xmlReaderAwaiter;
```

**48. Waiting for a Task with an XmlWriter Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create an xml writer awaiter.
XmlWriterAwaiter<int> xmlWriterAwaiter = new XmlWriterAwaiter<int>(task);

// Wait for the task to complete using the xml writer awaiter.
int result = await xmlWriterAwaiter;
```

**49. Waiting for a Task with a JsonReader Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a json reader awaiter.
JsonReaderAwaiter<int> jsonReaderAwaiter = new JsonReaderAwaiter<int>(task);

// Wait for the task to complete using the json reader awaiter.
int result = await jsonReaderAwaiter;
```

**50. Waiting for a Task with an JsonWriter Awaiter:**

```csharp
// Create a task.
Task<int> task = Task.Run(() => CalculateResult());

// Create a json writer awaiter.
JsonWriterAwaiter<int> jsonWriterAwaiter = new JsonWriterAwaiter<int>(task);

// Wait for the task to complete using the json writer awaiter.
int result = await jsonWriterAwaiter;
```
