Compiler Error CS4009

'Type.Method': an entry point cannot be marked with the async modifier.

You cannot use the async keyword in the application entry point (typically the Main method).

Important

Starting with C# 7.1, the Main method can have an async modifier. For more information, see Async main return values. For information about how to select the C# language version, see the Select the C# language version article.

Example

The following example produces CS4009:

using System;
using System.Threading.Tasks;

public class Example
{
    public static async void Main()
    {
        Console.WriteLine("About to wait two seconds");
        await WaitTwoSeconds();
        Console.WriteLine("About to exit the program");
    }

    private static async Task WaitTwoSeconds()
    {
        await Task.Delay(2000);
        Console.WriteLine("Returning from an asynchronous method");
    }
}

To correct this error

Update the C# language version used by the project to 7.1 or higher.

If you use C# 7.0 or lower, remove the async keyword from the signature of the application entry point. Also remove any await keywords you've used to await asynchronous methods in your application entry point.

However, you still have to wait for the asynchronous method to complete before your entry point resumes execution. Otherwise, compilation generates compiler warning CS4014, and the application will terminate before the asynchronous operation completes. The following example illustrates this problem:

using System;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
       Console.WriteLine("About to wait two seconds");
       WaitTwoSeconds();
       Console.WriteLine("About to exit the program");
   }

   private static async Task WaitTwoSeconds()
   {
      await Task.Delay(2000);
      Console.WriteLine("Returning from an asynchronous method");
   }
}
// The example displays the following output:
//       About to wait two seconds
//       About to exit the program

To await a method that returns a Task, call its Wait method, as the following example illustrates:

using System;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
       Console.WriteLine("About to wait two seconds");
       WaitTwoSeconds().Wait();
       Console.WriteLine("About to exit the program");
   }

   private static async Task WaitTwoSeconds()
   {
      await Task.Delay(2000);
      Console.WriteLine("Returning from an asynchronous method");
   }
}
// The example displays the following output:
//       About to wait two seconds
//       Returning from an asynchronous method
//       About to exit the program

To await a method that returns a Task<TResult>, retrieve the value of its Result property, as the following example does:

using System;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
       Console.WriteLine("About to wait two seconds");
       int value = WaitTwoSeconds().Result;
       Console.WriteLine($"Value returned from the async operation: {value}");
       Console.WriteLine("About to exit the program");
   }

   private static async Task<int> WaitTwoSeconds()
   {
      await Task.Delay(2000);
      Console.WriteLine("Returning from an asynchronous method");
      return 100;
   }
}
// The example displays the following output:
//       About to wait two seconds
//       Returning from an asynchronous method
//       Value returned from the async operation: 100
//       About to exit the program

See also