1

I'm making a web call using the async framework. I'm getting an Error noted in the code below

class Program
{
    static void Main(string[] args)
    {
        TestAsync async = new TestAsync();
        await async.Go();//Error:  the await operator can only be used with an async method.  Consider markign this method with the async modifier.  Consider applying the await operator to the result of the call
    }
}

class TestAsync
{
    public async Task Go()
    {
        using (WebClient client = new WebClient())
        {
            var myString =  await client.DownloadStringTaskAsync("http://msdn.microsoft.com");
            Console.WriteLine(myString);
        }    
    }
}

I've tried several variations of this code. It either fails at runtime or does not compile. In this case the method completes before my async call is allowed to fire. What am I doing wrong?

My goal is to execute a call to a web site using WebClient in an async fashion. I want to return the result as a string and print it out using Console.WriteLine. If you feel more comfortable starting with code that executes simply change

await async.Go(); to async.Go(); The code will run, but Console.WriteLine will not be hit.

4
  • 2
    Using await directly in Main() doesn't make any sense. What do you expect it to do? Commented Aug 21, 2012 at 19:58
  • I want to execute the web call asynchronously. The function is arbitrary I just want to setup a basic async call to learn the basics of the syntax. Feel free to make a web call in whatever way you feel appropriate. Commented Aug 21, 2012 at 19:59
  • Great resource for learning async in .net: msdn.microsoft.com/en-us/vstudio/hh378091 Commented Aug 21, 2012 at 20:09
  • BTW, using is not necessary with WebClient. Most of the time, you should make sure that you Dispose() anything that implements IDisposable, but WebClient has it only because its base (Component) does. Commented Aug 21, 2012 at 20:27

2 Answers 2

2

The error message is correctly telling you that await can only be used in async methods. But, you can't make Main() async, C# doesn't support that.

But async methods return Tasks, the same Task used in TPL since .Net 4.0. And Tasks do support synchronous waiting using the Wait() method. So, you can write your code like this:

class Program
{
    static void Main(string[] args)
    {
        TestAsync async = new TestAsync();
        async.Go().Wait();
    }
}

Using Wait() is the right solution here, but in other cases, mixing synchronous waiting using Wait() and asynchronous waiting using await can be dangerous and can lead to deadlocks (especially in GUI applications or in ASP.NET).

Sign up to request clarification or add additional context in comments.

1 Comment

I see, so the fact that I'm using a console application makes this a quirky little test. FYI, Task was introduced in .net 4.
-1

The program is ending before the web request can complete. This is because the Main will not wait for the async operation to complete since it has nothing left to do.

I bet if you make the Main last longer, then Console.WriteLine will get called.

I'd try adding a sleep after the call to the async method - Thread.Sleep(500) - anything long enough to permit the web request to complete should work.

4 Comments

A reasonable answer. But, I was expecting the async framework to offer up a blocking mechanism. await sounds like a block to me...
And what if it takes longer sometimes? Downloading some files may take several hours, does that mean I have to wait hours to download some small file too? And what happens if the downloading fails and throws an exception?
Agree, thread.sleep is a terrible way to synchronize threads. Not too bad for my code because its throw away.
I only offered this answer to explain why he wasn't seeing the Console.WriteLine, and offer a simple solution. In no way do I believe that Thread.Sleep should be used to synchronize threads, but it is perfectly valid to help you learn the new async functionality, without forcing a complete block like Wait() does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.