Asynchronous programming in C# allows tasks to run concurrently without blocking the main thread
, making applications
more responsive and efficient. This is particularly useful for operations that involve waiting, such as I/O operations
(file access, database queries, web requests), where blocking the main thread would lead to poor performance and a poor
user experience.
allows your application to handle multiple tasks concurrentl
y.
For example, a web server can handle multiple requests simultaneously without waiting for each request to complete
before starting the next one.user interface remains responsive while performing long-running operations in the background
.using System;using System.IO;using System.Threading.Tasks;class Program{static async Task Main(string[] args){Console.WriteLine("Starting file read...");// Start the file read operationstring content = await ReadFileAsync("file.txt");// This will run before the file reading is completedConsole.WriteLine("This will run before the file reading is completed.");// Print the file contentConsole.WriteLine(content);}static async Task<string> ReadFileAsync(string filePath){using (StreamReader reader = new StreamReader(filePath)){// Read the file content asynchronouslystring content = await reader.ReadToEndAsync();return content;}}}
The async and await keywords in C# are used to facilitate asynchronous programming, allowing you to write
asynchronous code that is easy to read and maintain. These keywords enable you to execute tasks without blocking the main thread
,
improving the responsiveness and performance of your applications.
The async keyword is used to declare a method as asynchronous
. The await keyword is used to
pause the executio
n of an asynchronous method until the awaited task completes
return Task or void
. Returning void is typically reserved for event handlers.using System;using System.IO;using System.Threading.Tasks;class Program{static async Task Main(string[] args){Console.WriteLine("Starting file read...");// Start the file read operation asynchronouslystring content = await ReadFileAsync("file.txt");// This will run after the file reading is completedConsole.WriteLine("File reading completed.");Console.WriteLine(content);}static async Task<string> ReadFileAsync(string filePath){using (StreamReader reader = new StreamReader(filePath)){// Read the file content asynchronouslystring content = await reader.ReadToEndAsync();return content;}}}
You can use try-catch blocks in asynchronous methods just like in synchronous methods. When an awaited task throws an exception, it is captured and can be handled within the try-catch block.
using System;using System.IO;using System.Threading.Tasks;class Program{static async Task Main(string[] args){try{string content = await ReadFileAsync("file.txt");Console.WriteLine(content);}catch (FileNotFoundException ex){Console.WriteLine($"File not found: {ex.Message}");}catch (Exception ex){Console.WriteLine($"An error occurred: {ex.Message}");}}static async Task<string> ReadFileAsync(string filePath){using (StreamReader reader = new StreamReader(filePath)){return await reader.ReadToEndAsync();}}}
Task.Run and Task.Factory.StartNew are both used to create and start tasks in C#, but they have
different default behaviors
and use cases. Understanding these differences is important to choose the right
method for your specific scenario.
lower-level method
that offers more control over task creation and scheduling.
It provides a wide range of options through its parameters
, which can be useful in advanced scenarios.need more control over task creation
, such as specifying a custom task scheduler,
setting task creation options, or passing a cancellation token.Task task = Task.Run(() => {// Perform some CPU-bound work hereConsole.WriteLine("Running on a background thread.");});Task.Factory.StartNew(() => {// Your code here},TaskCreationOptions.LongRunning); // Example of specifying a TaskCreationOption
Quick Links
Legal Stuff
Social Media