3

I have written a test app to help me understand await/async. I have a method MethodAsync1() which works fine. However when I call it from Main(), I cant figure out how to await for the MethodAsync1() to complete. See code below.

   class Program
    {
        static void Main(string[] args)
        {
            Debug.WriteLine(DateTime.Now + " Main()1");
            Task<String> v1 = MethodAsync1();
            Debug.WriteLine(DateTime.Now + " Main()2 - prints out before Method1 finishes");

            // I now want to wait for MethosAsync1() to complete before I go further.
            // This line has error:
            // Error    1   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'.  
            String v2 = await v1;

            Debug.WriteLine(DateTime.Now + " Main()3 - Method1() now finished");
        }



        static async Task<String> MethodAsync1()
        {
            Debug.WriteLine(DateTime.Now + " Method1()1 ");

            HttpClient client = new HttpClient();
            Task<HttpResponseMessage> responseTask = client.GetAsync("http://bbc.co.uk");

            // Do other stuff
            Debug.WriteLine(DateTime.Now + " Method1()2 ");

            // Wait for it to finish - yield thread back to Main()
            var response= await responseTask;

            // responseTask has completed - so thread goes back here and method returns fully
            Debug.WriteLine(DateTime.Now + " Method1()3 ");

            return "finished";
        }
    }
spiderplant0
  • 3,872
  • 12
  • 52
  • 91
  • Can you make `main` be an `async` method as well? – StriplingWarrior Jul 22 '14 at 18:16
  • No it will not let me it says: `'await1.Program.Main(string[])': an entry point cannot be marked with the 'async' modifier ` – spiderplant0 Jul 22 '14 at 18:17
  • Why should `main` be asynchronous? That makes no sense. Just block its thread until you get the result. – poke Jul 22 '14 at 18:17
  • @spiderplant0 You can't await in main. use `v1.Result` – EZI Jul 22 '14 at 18:18
  • Found the answer to my own question: http://stackoverflow.com/a/17579418/120955 – StriplingWarrior Jul 22 '14 at 18:18
  • Its such a nuisance when questions get closed of before folk have a chance to help you. The so called duplicate question no doubt provides an answer if you are familiar with c#, but for the inexperienced simply causes more confusion. – spiderplant0 Jul 22 '14 at 18:27
  • @spiderplant0 Do you really want to complain about people giving the answer you need? – JasonMArcher Jul 22 '14 at 18:47
  • Since I landed here first (note this is a duplicate question and therefore closed), I will comment that modern versions of C# (7.1 and above) support the desired behavior. Look at the duplicate question or this link for the answer. [C# documentation](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/await#await-operator-in-the-main-method) – Jubilsoft-Scott Oct 17 '22 at 15:10

1 Answers1

0

I don't think this is possible with the main method. Besides that it will be just a signature. In this specific case I will use the Result property of the Task in order to block until a result is given.

LazyOfT
  • 1,428
  • 7
  • 20