none
Using New Async/Await in Console App RRS feed

  • Question

  • I have a Class with Methods using the new async/await feature in .NET 4.5. I'm trying to use the Class in a Console App project, but seems to continue and not wait until the Method is completed.

    I cannot use the async keyword on Main and not sure how to set the Method call to wait until the Method is completed before moving on.

    Any samples is appreciated.

    Wednesday, September 12, 2012 10:37 PM

Answers

  • Hi Shaggygi,

    Welcome to the MSDN Forum.

    You can use the Task.Wait method to wait it finish:http://msdn.microsoft.com/en-us/library/dd235606.aspx 

            static void Main(string[] args)
            {
                Console.WriteLine("Main start");
                Task t = asyncMethod();
                t.ContinueWith((str) =>
                {
                    Console.WriteLine(str.Status.ToString());
                    Console.WriteLine("Main end");
                });
                t.Wait();
            }
    
            public async static Task<string> asyncMethod()
            {
                Console.WriteLine("public async static  void asyncMethod() start");
                await Task.Delay(100);
                Thread.Sleep(1000);
                Console.WriteLine("public async static  void asyncMethod() end");
                return "finished";
            }

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by shaggygi Thursday, September 13, 2012 1:22 PM
    Thursday, September 13, 2012 8:18 AM
    Moderator

All replies

  • below is a very simple way of waiting for an event to complete.  I doesn't use the new Async function but it is equivalent.  The code simple get the Host entry from the local computer which take a very short period of time.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Net;
    namespace ConsoleApplication1
    {
        class Program
        {
            static Boolean complete = false;
            static void Main(string[] args)
            {
                string host = "127.0.0.1";
                AsyncCallback callBack = new AsyncCallback(ProcessInformation);
                Dns.BeginGetHostEntry(host, callBack, host);
                //wait for function to complete
                while (!complete)
                {
                    Thread.Sleep(100);
                }
                Console.Write("done");
            }
            static void ProcessInformation(IAsyncResult result)
            {
                complete = true;
            }
        }
    }


    jdweng

    Thursday, September 13, 2012 7:24 AM
  • Hi Shaggygi,

    Welcome to the MSDN Forum.

    You can use the Task.Wait method to wait it finish:http://msdn.microsoft.com/en-us/library/dd235606.aspx 

            static void Main(string[] args)
            {
                Console.WriteLine("Main start");
                Task t = asyncMethod();
                t.ContinueWith((str) =>
                {
                    Console.WriteLine(str.Status.ToString());
                    Console.WriteLine("Main end");
                });
                t.Wait();
            }
    
            public async static Task<string> asyncMethod()
            {
                Console.WriteLine("public async static  void asyncMethod() start");
                await Task.Delay(100);
                Thread.Sleep(1000);
                Console.WriteLine("public async static  void asyncMethod() end");
                return "finished";
            }

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by shaggygi Thursday, September 13, 2012 1:22 PM
    Thursday, September 13, 2012 8:18 AM
    Moderator