Created
March 8, 2022 11:26
-
-
Save Abubakr0904/db72a743a40c3d92ff130a701199bfa3 to your computer and use it in GitHub Desktop.
async await example
This file contains hidden or 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; | |
| using System.Threading.Tasks; | |
| public class HelloWorld | |
| { | |
| public static async Task Main(string[] args) | |
| { | |
| Console.WriteLine ("Main Started."); | |
| var res = DoStuff(); | |
| Console.WriteLine ("Mainda DoStuffdan qaytgan task olindi."); | |
| await res; | |
| Console.WriteLine("Main tugadi."); | |
| } | |
| public static async Task DoStuff() | |
| { | |
| Console.WriteLine("DoStuff boshlandi."); | |
| var res = await Task.Run(() => DoStuff2()); | |
| Console.WriteLine($"Funksiya tugadi. Natija: {res}"); | |
| } | |
| public static long DoStuff2() | |
| { | |
| long sum = 0; | |
| for (long i = 0; i < 100000000; i++) | |
| { | |
| if(i % 2 == 0) | |
| sum -= i; | |
| else | |
| sum += i; | |
| } | |
| return sum; | |
| } | |
| } | |
| NATIJA: | |
| Main Started. | |
| DoStuff boshlandi. | |
| Mainda DoStuffdan qaytgan task olindi. | |
| Funksiya tugadi. Natija: 50000000 | |
| Main tugadi. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment