Ravi Kiran Chanduri

Ravi Kiran Chanduri

  • 1.1k
  • 1.3k
  • 1.1m

not executing code after Task in async/await programming

Dec 26 2019 10:45 AM
Hi ,
 
I am trying to understand the actual use of asyn/await in c# and have gone through few articles and tried with an example as below.
  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             Program p = new Program();  
  6.             p.MethodOne();  
  7.             p.MethodTwo();  
  8.             p.MethodThree();  
  9.         }  
  10.   
  11.         public void MethodOne()  
  12.         {  
  13.             Console.WriteLine("methodone has been called");  
  14.         }  
  15.         public async Task MethodTwo()  
  16.         {  
  17.             Task<string> delayingMethodCall = new Task<string>(DelayingMethod);  
  18.             Console.WriteLine("methodTwo call started");  
  19.             string message = await delayingMethodCall;  
  20.             Console.WriteLine(message);  
  21.             Console.WriteLine("methodTwo called");  
  22.             Console.ReadKey();  
  23.         }  
  24.         public void MethodThree()  
  25.         {  
  26.             Console.WriteLine("methodThree has been called");  
  27.         }  
  28.         public string DelayingMethod()  
  29.         {  
  30.             System.Threading.Thread.Sleep(5000);  
  31.             return "Delayed Process is done";  
  32.         }  
  33.     }  
 I am able to see third method's result , but the code after string message = await delayingMethodCall;
is not executing.

Can someone explain me the issue here?
 
Thanks in Advance 
Ravi. 

Answers (3)