有关与异步和等待C#异步编程问题 [英] Issue regarding asynchronous Programming with Async and Await c#

查看:139
本文介绍了有关与异步和等待C#异步编程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何使用异步并等待C#。所以我有一个链接<一个href=\"http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx#BKMK_WhatHappensUnderstandinganAsyncMethod\" rel=\"nofollow\">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.

这篇关于有关与异步和等待C#异步编程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆