有关与异步和等待C#异步编程问题

有关与异步和等待C#异步编程问题

问题描述:

我正在学习如何使用异步并等待C#。所以我有一个链接http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx#BKMK_WhatHappensUnderstandinganAsyncMethod

i was learning how to use Async and Await c#. so i got a link http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx#BKMK_WhatHappensUnderstandinganAsyncMethod

在这里,我尝试从VS2012 IDE,但得到错误code。
这个功能是提高错误。

from here i try to run the code from VS2012 IDE but getting error. this function is raising error.

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);
        }

此行给错误等待AccessTheWebAsync();
在'等待'运算符只能异步方法中使用。考虑标志着该方法与异步修饰和改变它的返回类型为'任务'

我在做什么错。请指导我如何运行code。谢谢

what i am doing the wrong. please guide me how to run the code. thanks

这很清楚地指出,你必须用装饰你的方法异步。像这样:

It very clearly states that you have to decorate your method with async. Like so:

// note the 'async'!
private async void button1_Click(object sender, EventArgs e)

看看这里:异步(C#参考)

通过使用异步修饰符,您可以指定一个方法,拉姆达前pression,或匿名方法是异步的。如果您使用此修改器的方法或前pression,它被称为异步方法。

By using the async modifier, you specify that a method, lambda expression, or anonymous method is asynchronous. If you use this modifier on a method or expression, it's referred to as an async method.