none
How to handle the async modify main method RRS feed

  • Question

  •  static async void Main(string[] args)
            {
                Task<string> getWebPageTask = GetWebPageAsync("xxx");

                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);

                Debug.WriteLine("In GetWebPageAsync before await");
                string webText = await getStringTask;
                Debug.WriteLine("In GetWebPageAsync after await");
                return webText;
            }
    Wednesday, December 20, 2017 3:31 PM

Answers

  • Hello Aimee,

    >>How to handle the async modify main method

    For a Console program, You use "async" keyword to modify the "Main" method will cause some issues. As Stephen Cleary said, this works perfectly in UI applications (the method just returns to the UI event loop) and ASP.NET applications (the method returns off the thread but keeps the request alive). It doesn’t work out so well for Console programs: Main returns to the OS. So if you insist on using "async" to modify Main method. you could use AsyncContext. The code should be like as below.

    class Program
    {
        static void Main(string[] args)
        {
            AsyncContext.Run(() => MainAsync(args));
        }
    
        static async void MainAsync(string[] args)
        {
              ...
              ...     
        }
    }

    Or you update the C# language to C# 7.1, which will be able to do a proper async Main. you could get more detailed information from there.

    https://stackoverflow.com/questions/9208921/cant-specify-the-async-modifier-on-the-main-method-of-a-console-app

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Aimee.Young Thursday, December 21, 2017 3:02 PM
    Thursday, December 21, 2017 2:34 AM
    Moderator

All replies

  • What exactly is your problem with this code?
    Wednesday, December 20, 2017 4:12 PM
  • Hello Aimee,

    >>How to handle the async modify main method

    For a Console program, You use "async" keyword to modify the "Main" method will cause some issues. As Stephen Cleary said, this works perfectly in UI applications (the method just returns to the UI event loop) and ASP.NET applications (the method returns off the thread but keeps the request alive). It doesn’t work out so well for Console programs: Main returns to the OS. So if you insist on using "async" to modify Main method. you could use AsyncContext. The code should be like as below.

    class Program
    {
        static void Main(string[] args)
        {
            AsyncContext.Run(() => MainAsync(args));
        }
    
        static async void MainAsync(string[] args)
        {
              ...
              ...     
        }
    }

    Or you update the C# language to C# 7.1, which will be able to do a proper async Main. you could get more detailed information from there.

    https://stackoverflow.com/questions/9208921/cant-specify-the-async-modifier-on-the-main-method-of-a-console-app

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Aimee.Young Thursday, December 21, 2017 3:02 PM
    Thursday, December 21, 2017 2:34 AM
    Moderator