A repository of exercises to support the training.
In this module, you will learn how to implement asynchronous programming using async
and await
. You'll explore how futuristic city systems can perform long-running tasks efficiently without blocking resources.
Imagine a futuristic city's resource management system that needs to perform computationally intensive tasks such as energy load calculations and infrastructure monitoring. Asynchronous programming can ensure that these tasks run efficiently without delaying other operations.
Instructions:
async
and await
to execute the task without blocking the main thread.using System;
using System.Threading;
using System.Threading.Tasks;
public class FuturisticCity
{
public static int LongCalculation()
{
Console.WriteLine("Starting LongCalculation...");
Thread.Sleep(3000); // Simulate a delay
Console.WriteLine("LongCalculation completed.");
return 42; // Return a result after the calculation
}
public static async Task<int> CalculateAsync()
{
int result = await Task.Run(() => LongCalculation());
Console.WriteLine($"Calculation result: {result}");
return result;
}
}
public static async Task Task1()
{
Console.WriteLine("Task1: Monitoring energy grid...");
await Task.Delay(2000); // Simulate task delay
Console.WriteLine("Task1 completed.");
}
public static async Task Task2()
{
Console.WriteLine("Task2: Analyzing infrastructure...");
await Task.Delay(3000); // Simulate task delay
Console.WriteLine("Task2 completed.");
}
public static async Task RunTasksSequentiallyAsync()
{
Console.WriteLine("Running tasks sequentially...");
await Task1();
await Task2();
Console.WriteLine("Sequential tasks completed.");
}
public static async Task RunTasksConcurrentlyAsync()
{
Console.WriteLine("Running tasks concurrently...");
var task1 = Task1();
var task2 = Task2();
await Task.WhenAll(task1, task2);
Console.WriteLine("Concurrent tasks completed.");
}
Expected Outcome:
Next Steps:
Happy awaiting!