Tasks and Async
Tasks
using System;
using System.Threading.Tasks;
public class Program
{
public static void Main()
{
Task<int> task = Task.Run(() =>
{
// Simulate some time-consuming work
System.Threading.Thread.Sleep(2000);
return 42;
});
Console.WriteLine("Main thread is not blocked.");
int result = task.Result; // This will block until the task completes
Console.WriteLine("The result is: " + result);
}
}Async methods
Last updated