Async and await are markers which marked code positions from where control should resumed after a task (thread) completes.It is introduced in .net 4.5 framework.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace AsyncANDAwait
{
class Program
{
static void Main(string[] args)
{
Method();
Console.WriteLine("Main Method called");
Console.ReadLine();
}
static async void Method()
{
await Task.Run(new Action(LongTaskMethod));
Console.WriteLine("AsyncMethod called"); //wait until long task finished.
}
static void LongTaskMethod()
{
Thread.Sleep(20000);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace AsyncANDAwait
{
class Program
{
static void Main(string[] args)
{
Method();
Console.WriteLine("Main Method called");
Console.ReadLine();
}
static async void Method()
{
await Task.Run(new Action(LongTaskMethod));
Console.WriteLine("AsyncMethod called"); //wait until long task finished.
}
static void LongTaskMethod()
{
Thread.Sleep(20000);
}
}
}
No comments:
Post a Comment