关于异步编程与异步和等待c#的问题

Tho*_*mas -1 c# asynchronous async-await

我正在学习如何使用Async和Await c#.所以我有一个链接http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx#BKMK_WhatHappensUnderstandinganAsyncMethod

从这里我尝试从VS2012 IDE运行代码,但收到错误.此功能引发错误.

private void button1_Click(object sender, EventArgs e)
        {
            int contentLength = await AccessTheWebAsync();

            label1.Text= String.Format("\r\nLength of the downloaded string: {0}.\r\n", contentLength);
        }
Run Code Online (Sandbox Code Playgroud)

此行给出错误await AccessTheWebAsync(); 'await'运算符只能在异步方法中使用.考虑使用'async'修饰符标记此方法并将其返回类型更改为'Task'

我做错了什么.请指导我如何运行代码.谢谢

ger*_*rmi 5

它非常清楚地表明你必须用你的方法来装饰你的方法async.像这样:

// note the 'async'!
private async void button1_Click(object sender, EventArgs e)
Run Code Online (Sandbox Code Playgroud)

看看这里:async(C#参考).

通过使用async修饰符,可以指定方法,lambda表达式或匿名方法是异步的.如果在方法或表达式上使用此修饰符,则将其称为异步方法.