none
Error using await function RRS feed

  • Question

  • I am trying to use the new await funtion in C# but am getting an error saying 'Cannot await string'

    This is how I am writing the code (note this is a simpe example that just converts a string, for brevity sake):

    Task<string> resultTask = DoThisAsync("The big brown fox");
    var myString = await resultTask;
           
    private async Task<string> DoThisAsync(string value)      
    {
     return await value.ToUpper(); //this is where the error is
    }

    Friday, August 31, 2012 11:23 AM

Answers


  • Task<string> resultTask = DoThisAsync("The big brown fox");
    var myString = await resultTask;
           
    private async Task<string> DoThisAsync(string value)      
    {
     return await value.ToUpper(); //this is where the error is
    }

    The problem is that await requires something awaitable, such as a Task<T>, to await on.  value.ToUpper() is a synchronous operation.

    In this case, using await here is overkill, as string.ToUpper() is such as fast operation that there is no reason to make this asynchronous.  If you were doing something that required more work, however, you could get around this by wrapping it into a Task:

    Task<string> resultTask = DoThisAsync("The big brown fox");
    var myString = await resultTask;
            
    private async Task<string> DoThisAsync(string value)       
    {
       return await Task.Run( () =>
       {
           string newValue = ComputeValue(value); // This would have to take time to make this worthwhile
           return newValue.ToUpper();
       });
    }


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".


    Friday, August 31, 2012 4:31 PM
    Moderator

All replies

  • Hi,

    as you have a question, please change the thread type from discussion to question.

    To your problem:

    remove the await at the offending line.

    See also: http://msdn.microsoft.com/en-us/library/hh156528.aspx


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Friday, August 31, 2012 11:33 AM
  • But by removing it, it would then run synchronously, and the function gets this warning:

    This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread

    Friday, August 31, 2012 11:38 AM
  • Yes you are right. But for your sample using a async task is just an overkill. If you still want to do this you can use it like await Task.Delay(1) . The await keyword just needs to be applied to a Task and not anything else.

    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Friday, August 31, 2012 12:22 PM

  • Task<string> resultTask = DoThisAsync("The big brown fox");
    var myString = await resultTask;
           
    private async Task<string> DoThisAsync(string value)      
    {
     return await value.ToUpper(); //this is where the error is
    }

    The problem is that await requires something awaitable, such as a Task<T>, to await on.  value.ToUpper() is a synchronous operation.

    In this case, using await here is overkill, as string.ToUpper() is such as fast operation that there is no reason to make this asynchronous.  If you were doing something that required more work, however, you could get around this by wrapping it into a Task:

    Task<string> resultTask = DoThisAsync("The big brown fox");
    var myString = await resultTask;
            
    private async Task<string> DoThisAsync(string value)       
    {
       return await Task.Run( () =>
       {
           string newValue = ComputeValue(value); // This would have to take time to make this worthwhile
           return newValue.ToUpper();
       });
    }


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".


    Friday, August 31, 2012 4:31 PM
    Moderator
  • Hi Reed,

    Thanks. This works a treat. I know it seemed overkill for what I was trying, but it was only the example. I am doing a long winded function instead of toUpper(), I only did this for brevity sake.

    Thanks again, it all makes sense now.

    Tuesday, September 4, 2012 8:15 AM