56

I copied below code from this link.But when I am compiling this code I am getting an entry point cannot be marked with the 'async' modifier. How can I make this code compilable?

class Program
{
    static async void 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()); 
    }

    private static async Task<string> GetWebPageAsync(string url)
    {
        // Start an async task. 
        Task<string> getStringTask = (new HttpClient()).GetStringAsync(url);

        // Await the task. This is what happens: 
        // 1. Execution immediately returns to the calling method, returning a 
        //    different task from the task created in the previous statement. 
        //    Execution in this method is suspended. 
        // 2. When the task created in the previous statement completes, the 
        //    result from the GetStringAsync method is produced by the Await 
        //    statement, and execution continues within this method. 
        Debug.WriteLine("In GetWebPageAsync before await");
        string webText = await getStringTask;
        Debug.WriteLine("In GetWebPageAsync after await");

        return webText;
    }

    // Output: 
    //   In GetWebPageAsync before await 
    //   In startButton_Click before await 
    //   In GetWebPageAsync after await 
    //   Characters received: 44306
}
svick
  • 236,525
  • 50
  • 385
  • 514
  • 1
    You can't mark `Main` with async. – Jcl May 23 '13 at 11:03
  • 1
    @JCL:How I can call async/awai method in main method –  May 23 '13 at 11:04
  • You can find all the information about async and await on [this link](http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx). I'm not sure you are getting the concept right. – Jcl May 23 '13 at 11:08
  • I know in wpf it's working fine.but for demo I have created console and I want to test in console –  May 23 '13 at 11:19
  • 2
    Just get the code you want to call asynchronously out of the `Main` function, and call your function from the code inside `Main`. – Jcl May 23 '13 at 11:23
  • I have a [blog post on the subject](http://blog.stephencleary.com/2012/02/async-console-programs.html). You can either install your own context (as I do in my post) or just use `Task.Wait` (as svick does in his answer). – Stephen Cleary May 23 '13 at 12:33
  • @user2408588, just for argumente sake, what would you expect to happen if the entry point could be an async method? – Paulo Morgado May 24 '13 at 01:28
  • Possible duplicate of [Can't specify the 'async' modifier on the 'Main' method of a console app](https://stackoverflow.com/questions/9208921/cant-specify-the-async-modifier-on-the-main-method-of-a-console-app) – Michael Freidgeim Nov 14 '17 at 22:41

5 Answers5

92

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.

spottedmahn
  • 14,823
  • 13
  • 108
  • 178
svick
  • 236,525
  • 50
  • 385
  • 514
  • 1
    wait() is void. how to get the result AFTER WAITING ? – Bilal Fazlani Jul 16 '13 at 08:37
  • 9
    @bilalfazlani What kind of result? `Main()` usually doesn't return anything. In any case, if you have a `Task` (i.e. `Task` with a result), you can use `Result` instead of `Wait()`. – svick Jul 16 '13 at 08:50
22

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.

Roman
  • 11,966
  • 10
  • 38
  • 47
  • 2
    As a footnote, you need to change your project build settings to use the specific minor C# version or it'll default to C#7.0 instead. This will enable you to use the `async` keyword on `Main`. – Maximilian Burszley May 10 '19 at 14:06
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);
}
M. Hamza Rajput
  • 7,810
  • 2
  • 41
  • 36
1

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.

Igal Tabachnik
  • 31,174
  • 15
  • 92
  • 157
1

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

dferenc
  • 7,918
  • 12
  • 41
  • 49