Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An entry point cannot be marked with the 'async' modifier

People also ask

What does the async modifier do?

The async keyword is contextual in that it's a keyword only when it modifies a method, a lambda expression, or an anonymous method. In all other contexts, it's interpreted as an identifier.

Why you shouldn't use async void?

Async void methods can wreak havoc if the caller isn't expecting them to be async. When the return type is Task, the caller knows it's dealing with a future operation; when the return type is void, the caller might assume the method is complete by the time it returns.

Can an async function return a value C#?

Starting with C# 7.0, an async method can return any type that has an accessible GetAwaiter method that returns an instance of an awaiter type.

What happens when async method is not awaited?

The call to the async method starts an asynchronous task. However, because no Await operator is applied, the program continues without waiting for the task to complete. In most cases, that behavior isn't expected.


The error message is exactly right: the Main() method cannot be async, because when Main() returns, the application usually ends.

If you want to make a console application that uses async, a simple solution is to create an async version of Main() and synchronously Wait() on that from the real Main():

static void Main()
{
    MainAsync().Wait();
}

static async Task MainAsync()
{
    // your async code here
}

This is one of the rare cases where mixing await and Wait() is a good idea, you shouldn't usually do that.

Update: Async Main is supported in C# 7.1.


Starting from C# 7.1 there are 4 new signatures for Main method which allow to make it async(Source, Source 2, Source 3):

public static Task Main();
public static Task<int> Main();
public static Task Main(string[] args);
public static Task<int> Main(string[] args);

You can mark your Main method with async keyword and use await inside Main:

static async Task Main(string[] args)
{
    Task<string> getWebPageTask = GetWebPageAsync("http://msdn.microsoft.com");

    Debug.WriteLine("In startButton_Click before await");
    string webText = await getWebPageTask;
    Debug.WriteLine("Characters received: " + webText.Length.ToString()); 
}

C# 7.1 is available in Visual Studio 2017 15.3.


I'm using C# 8 and its working fine.

static async Task Main(string[] args)
{
   var response = await SomeAsyncFunc();
   Console.WriteLine("Async response", response);
}

OR without "await" keyword.

static void Main(string[] args)
{
   var response = SomeAsyncFunc().GetAwaiter().GetResult();
   Console.WriteLine("Async response", response);
}

The difference between the code in the link's example and yours, is that you're trying to mark the Main() method with an async modifier - this is not allowed, and the error says that exactly - the Main() method is the "entry point" to the application (it's the method that is executed when your application starts), and it's not allowed to be async.


Wrap your async code in MainAsync() - which is an async function
then call MainAsync().GetAwaiter().GetResult();