using System;
using System.Threading;
using System.Threading.Tasks;
namespace AsyncAwaitConsole
{
class Program
{
static void Main(string[] args)
{
// What are Async and Await ( .NET 4.5 Interview question with answers)?
// https://www.youtube.com/watch?v=V2sMXJnDEjM
// Async Await are markers showing where the control resumes after the task or thread completes
Method(); // only method will wait for the LongTask to complete
Console.WriteLine("Main thread"); // the main thread is executed
Console.ReadLine();
}
public static async void Method() // You have to use async if you use await
{
await Task.Run(new Action(LongTask)); // invoces LongTask in paralel mode
// but what we want is actully to execute the next line only when LongTask is finished.
Console.WriteLine("New Thread"); // Wait until the long task finishes
}
public static void LongTask()
{
Thread.Sleep(20000);
}
}
}
/*
Main thread
New Thread
dasd
*/
Comments