Wednesday, February 3, 2021

How can I call an async method in Main?

That kind if issue are facing beginner and those developer are not familiar with async and await used.
So in this post i have explain you how to call async method.

 
namespace PracticeHemaware
{
class Program
{
private static string result = "123";

static void Main(string[] args)
{
var data= Task.Run(async () => await saytest());
// saytest(result).Wait();
Console.WriteLine(data.Result);

Console.Read();
}
static async Task<string> saytest()
{
await Task.Delay(5);
result = "Hello World";
return result;
}
}
}
this is my code and saytest() is my async method.

Thank you.

No comments:

Post a Comment