Async CTP - Type System.Threading.Tasks.Task<int> is not awaitable

Locked Async CTP - Type System.Threading.Tasks.Task<int> is not awaitable

  • Friday, December 30, 2011 9:10 AM
     
      Has Code

    Hello Community,

    i install the last Async CTP version and like use it on VS 2010 SP1 - .NET 4.

    The follow Code dosn´t work:

    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine("Start");
    
            await Test();
    
            Console.WriteLine("Ende");
            Console.ReadLine();
        }
    
        public static async Task<int> Test()
        {
            Thread.Sleep(TimeSpan.FromSeconds(5));
    
            return 7;
        }
    }
    

    Visual Studio shows a error on await with "Type System.Threading.Tasks.Task<int> is not awaitable". Why?
    On every "Get Started" use the Task for awaitable actions.

    Any ideas?!

All Replies

  • Friday, December 30, 2011 2:30 PM
     
     

    Did you add a reference to AsyncCtpLibrary.dll? It's under My Documents\Microsoft Visual Studio Async CTP\Samples.

            -Steve


    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ
      and How to Implement IDisposable and Finalizers: 3 Easy Rules
    Microsoft Certified Professional Developer

    How to get to Heaven according to the Bible
  • Friday, December 30, 2011 3:03 PM
     
     
    yes, is referenced... Visual Studio accept the async declaration but the await get the error message... with open the samples is the same problem...
  • Saturday, December 31, 2011 7:06 PM
    Moderator
     
     Proposed Answer

    I'm not sure why you're seeing that particular error message, but you can't use "await Test();" inside of a method that's not marked as "async"... and even if you were to add "async" to the contianing method, you also can't use it inside of your Main method.

  • Tuesday, January 03, 2012 4:17 PM
     
     Proposed Answer Has Code

    yes, that was a copy of code without async inside of method.. but with async is the same problem on await with other error message "Type 'void' is not awaitable".

    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine("Start");
            
            Test();
    
            Console.WriteLine("Ende");
            Console.ReadLine();
        }
    
        public static async Task<int> Test()
        {
            await Thread.Sleep(TimeSpan.FromSeconds(5));
    
            return 7;
        }
    }
    

     

    • Proposed As Answer by jtorrecillaMVP Sunday, January 29, 2012 8:28 AM
    •  
  • Tuesday, January 03, 2012 4:48 PM
    Moderator
     
     Proposed Answer

    That message is valid... Thread.Sleep returns void, and that's not something you can await.  Did you mean "await TaskEx.Delay(TimeSpan.FromSeconds(5));"?

    Also, once you make that change, your Main method is going to invoke Test, which will return immediately, such that you'll be printing out "Ende" before the asynchronous operation completes.  As such, you probably want to change your invocation of Test from "Test();" to "Test().Wait();" so that you don't progress to print out "Ende" until the asynchronous operation completes.

  • Tuesday, January 03, 2012 5:03 PM
     
      Has Code

    Hey Stephen,

    thanks for your fast answer and for the Wait-Method hint.

    If i used TaskEx.Delay get the Message: "Type 'System.Threading.Tasks.Taks' is not awaitable" on await.

    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine("Start");
            
            Test().Wait();
    
            Console.WriteLine("Ende");
            Console.ReadLine();
        }
    
        public static async Task<int> Test()
        {
            await TaskEx.Delay(TimeSpan.FromSeconds(5));
    
            return 7;
        }
    }
    

    The same is with async decalration on Main-Method.

  • Monday, January 16, 2012 4:33 PM
    Moderator
     
     Answered

    It's not clear to me from your code shown what the problem is.  What's the entire contents of your file, including using statements?  It could just be that something is wrong with your installation.

  • Thursday, February 16, 2012 11:09 AM
     
      Has Code
        private static void Main(string[] args)
        {
            Console.WriteLine("Start");
            Test();
    
            Console.WriteLine("Ende");
            Console.ReadLine();
        }
    
        private async Task<int> Test()
        {
            await TaskEx.Run(() =>TimeSpan.FromSeconds(5));
    
            return 7;
        }
    hope this helps