0 day streak
csharp / beginner
Snippet

Async Method Execution

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

snippet.cs
csharp
1
2
3
4
5
6
7
8
9
10
11
using System;
using System.Threading.Tasks;
public class TaskRunner {
public async Task RunProcessAsync() {
Console.WriteLine("Starting process...");
// Await pauses the method without blocking the thread
await Task.Delay(500);
Console.WriteLine("Process completed.");
}
}
Breakdown
1
public async Task RunProcessAsync()
The 'async' keyword marks the method as asynchronous, and 'Task' is the return type for operations that don't return a value.
2
await Task.Delay(500);
The 'await' keyword tells the program to wait for the task to finish before moving to the next line.