0 day streak
csharp / beginner
Snippet

Asynchronous Delay Management

Async and await allow the program to handle long-running tasks without freezing the main thread. Task.Delay is a common way to simulate work asynchronously.

snippet.cs
csharp
1
2
3
4
5
6
7
8
9
10
11
12
using System;
using System.Threading.Tasks;
public class AsyncDemo
{
public async Task ProcessDataAsync()
{
// Non-blocking wait for 1 second
await Task.Delay(1000);
Console.WriteLine("Processing complete.");
}
}
Breakdown
1
public async Task ProcessDataAsync()
Defines a method that can use the await keyword and returns a Task.
2
await Task.Delay(1000);
Suspends the method execution for 1000ms without blocking the thread.