using System;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
CallWithAsync();
Console.WriteLine("Press any key to quit.");
Console.ReadKey();
}
// common method
private static string GetData(string actionName)
{
// Do your heavy work here. E.g. fetch data from a database.
Thread.Sleep(3000);
return string.Format("Action {0} finished. The time is {1}.", actionName, DateTime.Now.TimeOfDay);
}
// WRAPPER for async methods: these will make "GetData" asynchronous.
private static Task<string> GetDataAsync(string name){
// wrap the GetData method in a Task:
return Task.Run<string>(() =>
{
return GetData(name);
});
}
private async static void CallWithAsync()
{
// Call the async method and wait for its result
string result = await GetDataAsync("Custom Action Name");
Console.WriteLine(result);
}
}
}