Calling Async Call from Main()
This is a small code which shows different ways of calling an async method inside Main
Following is my sample method.
- public async Task<int>SampleMethod()
- {
- await Task.Delay(5000);
- int answer = 21 * 2;
- return answer;
- }
1. Using GetAwaiter()
- static void Main(string[] args)
- {
- SampleMethod().GetAwaiter().OnCompleted(() =>
- {
- Console.WriteLine("finished");
- });
- Console.ReadKey();
- }
2. Using Wait()
- static void Main(string[] args)
- {
- SampleMethod().Wait();
- }
Hope this helps,
Comments
Post a Comment