Advertisement
wingman007

AsyncAwaitDotNet

Nov 24th, 2021
871
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.06 KB | None | 0 0
  1. using System;
  2. using System.Net.Http;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5.  
  6. // created using
  7. // https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/task-asynchronous-programming-model
  8.  
  9. namespace AsyncAwait
  10. {
  11.     internal class Program
  12.     {
  13.         static async Task Main(string[] args)
  14.         {
  15.             int result = await GetUrlContentLengthAsync();
  16.  
  17.             Console.WriteLine("Hello World! string count = {0}", result);
  18.  
  19.         }
  20.         static async public Task<int> GetUrlContentLengthAsync()
  21.         {
  22.             var client = new HttpClient();
  23.  
  24.             DoIndependentWork();
  25.  
  26.             // 1. Aproach with Task
  27.             Task<string> getStringTask =
  28.                 client.GetStringAsync("https://docs.microsoft.com/dotnet");
  29.             string contents = await getStringTask;
  30.  
  31.             // 2.
  32.             // Direct aproach
  33.             //string contents =
  34.             //    await client.GetStringAsync("https://docs.microsoft.com/dotnet");
  35.  
  36.             // 3. Do long syncronous job
  37.             // var contents = DoLong(); // !!! Without await into the bory the method is syncronous
  38.  
  39.             // 4. Async ordinar long running method without async in the declaration
  40.             // 4.1. DoLomg retuns void
  41.             // Task<int> result = await Task.Run(new Action(DoLong)); // if DoLong retuns void
  42.  
  43.             // 4.2.
  44.             // string contents = await Task<string>.Run(DoLong);
  45.  
  46.  
  47.  
  48.             Task<int> returnedTaskTResult = GetTaskOfTResultAsync();
  49.             int intResult = await returnedTaskTResult;
  50.             // Single line
  51.             // int intResult = await GetTaskOfTResultAsync();
  52.  
  53.             Task returnedTask = GetTaskAsync();
  54.             await returnedTask;
  55.             // Single line
  56.             await GetTaskAsync();
  57.  
  58.  
  59.             return contents.Length;
  60.         }
  61.  
  62.         static void DoIndependentWork()
  63.         {
  64.             Console.WriteLine("Working...");
  65.         }
  66.  
  67.         // 3.
  68.         //static string DoLong() // Even if the method is not declared async can be used as async with
  69.         //{
  70.         //    Thread.Sleep(10000);
  71.         //    return "Stoyan";
  72.         //}
  73.  
  74.         // 4.1.
  75.         //static void DoLong() // Even if the method is not declared async can be used as async with
  76.         //{
  77.         //    Thread.Sleep(10000);
  78.         //    // return "Stoyan";
  79.         //}
  80.  
  81.         // 4.2.
  82.         //static Task<string> DoLong() // Even if the method is not declared async can be used as async with
  83.         //{
  84.         //    Thread.Sleep(10000);
  85.         //    // return "Stoyan";
  86.         //    Task<string> task = new Task<string>(() => { return "Stoyan"; }) ;
  87.         //    return task;
  88.         //}
  89.  
  90.         static async Task<int> GetTaskOfTResultAsync()
  91.         {
  92.             int hours = 0;
  93.             await Task.Delay(0);
  94.  
  95.             return hours;
  96.         }
  97.  
  98.  
  99.  
  100.         static async Task GetTaskAsync()
  101.         {
  102.             await Task.Delay(0);
  103.             // No return statement needed
  104.         }
  105.     }
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement