Created
December 30, 2022 08:14
-
-
Save varun-r-boop/0c1b13e6d90489dc662a46c35978b248 to your computer and use it in GitHub Desktop.
Asynchoronus Programming:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Threading.Tasks; | |
namespace AsyncExample | |
{ | |
public class Program | |
{ | |
public static async Task Main(string[] args) | |
{ | |
// Call an async method and wait for it to complete | |
int result = await AsyncMethod(); | |
Console.WriteLine(result); | |
} | |
public static async Task<int> AsyncMethod() | |
{ | |
// Run some code asynchronously | |
int result = await Task.Run(() => { | |
// Simulate a long-running task by sleeping for 2 seconds | |
System.Threading.Thread.Sleep(2000); | |
return 42; | |
}); | |
// Return the result | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment