0 day streak
csharp / beginner
Snippet

Asynchronous Delay with Task

Asynchronous programming allows your code to wait for operations like network requests or timers without freezing the entire application.

snippet.cs
csharp
1
2
3
4
5
6
7
8
9
10
using System;
using System.Threading.Tasks;
public class AsyncDemo {
public static async Task RunExample() {
Console.WriteLine("Waiting started...");
await Task.Delay(2000);
Console.WriteLine("Finished after 2 seconds.");
}
}
Breakdown
1
async Task
Marks the method as asynchronous, allowing the use of the await keyword.
2
await Task.Delay(2000)
Yields control back to the caller for 2000 milliseconds before resuming.