Await Completed Task - CSharp Thread Asynchronous

CSharp examples for Thread Asynchronous:Task

Description

Await Completed Task

Demo Code

using System;/*w  w w.j  ava 2s . c  o  m*/
using System.ComponentModel;
using System.Threading.Tasks;
class AwaitCompletedTask
{
   static void Main()
   {
      Task task = DemoCompletedAsync();
      Console.WriteLine("Method returned");
      // Safe in a console app - no synchronization context
      task.Wait();
      Console.WriteLine("Task completed");
   }
   static async Task DemoCompletedAsync()
   {
      Console.WriteLine("Before first await");
      await Task.FromResult(10);
      Console.WriteLine("Between awaits");
      await Task.Delay(1000);
      Console.WriteLine("After second await");
   }
}

Result


Related Tutorials