I've got a very simple code snippet, to test how to call Task<> method in Main()
using System;
using System.Threading;
using System.Threading.Tasks;
class Program
{
private static async Task<int> F1(int wait = 1)
{
await Task.Run(() => Thread.Sleep(wait));
Console.WriteLine("finish {0}", wait);
return 1;
}
public static async void Main(string[] args)
{
Console.WriteLine("Hello World!");
var task = F1();
int f1 = await task;
Console.WriteLine(f1);
}
}
It doesn't compile because:
(1) F1 is async, so Main() has to be "async".
(2) Compiler says :
error CS5001: Program does not contain a static 'Main' method suitable for an entry point
So if I remove the "async" of Main, compiler will say:
error CS4033: The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.
I can either add or remove "async" keyword here. How to make it work? Thanks a lot.