寻找指导,以了解与异步如何异步编程并等待作品 [英] Looking for guidance to understand how asynchronous Programming with Async and Await works

查看:109
本文介绍了寻找指导,以了解与异步如何异步编程并等待作品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经过一个MSDN示例code,其中被点击时键,当程序被调用,则使用等待关键字和功能使用异步关键字函数被调用。

 专用异步无效StartButton_Click(对象发件人,RoutedEventArgs E)
        {            INT CONTENTLENGTH =等待AccessTheWebAsync();            resultsTextBox.Text + =
                的String.Format(\\ r \\ nLength下载的字符串:{0} \\ r \\ n,CONTENTLENGTH);
        }异步任务< INT> AccessTheWebAsync()
        {
            HttpClient的客户端=新的HttpClient();
            任务<串GT; getStringTask = client.GetStringAsync(http://msdn.microsoft.com);
            DoIndependentWork();
            字符串urlContents =等待getStringTask;            返回urlContents.Length;
        }
        无效DoIndependentWork()
        {
            resultsTextBox.Text + =工作\\ r \\ n。。。。
        }


  1. AccessTheWebAsync 被称为然后等待使用关键字,这是什么意思?

  2. 当此功能AccessTheWebAsync()将被执行,然后 DoIndependentWork()函数被调用,我在这里想控制将等待,直到此功能 DoIndependentWork ()结束。我说得对不对?

再有一个叫另一个语句

 字符串urlContents =等待getStringTask;

他们为什么在这里使用等待。如果我们不使用那么这里等待会发生什么事?

请指引我理解code是它是如何工作的。


解决方案

下面是关于异步/ await方法的简要说明。

异步方法:


  • 来电不一定阻塞异步的全部执行
    方法

  • 可能的返回类型

    • 无效:发射后不管

    • 任务:允许等待异步方法终止

    • 任务< T> :允许等待终止并获得T型结果


  • 没有ref或out参数为异步方法

  • 必须再次包含的await =>否则编译警告

指日可待任务


  • 等待下TPL任务终止

  • 返回结果的任务(如使用任务
    结果类型)

  • 必须只出现在异步方法=>否则编译出错

这是异步方法部分是同步和异步部分


  • 来电同步执行,直至阻塞的await
  • 方法
  • 此后,异步执行的方法休息
    异步和放大器;等待机制
    高效

 公共异步任务< INT> GetSiteLengthAsync(字符串URL)
{
    HttpClient的客户端=新的HttpClient(); < =同步
    任务<串GT;下载1 = client.GetStringAsync(URL); < =同步
    串站点1 =等待下载1; < =异步(另一个线程)
    返回site1.Length; < =异步(另一个线程)
}

i go through a msdn sample code where a function is called when button is clicked and when routine is called then Await keyword is used and function has async keyword used.

private async void StartButton_Click(object sender, RoutedEventArgs e)
        {

            int contentLength = await AccessTheWebAsync();

            resultsTextBox.Text +=
                String.Format("\r\nLength of the downloaded string: {0}.\r\n", contentLength);
        }

async Task<int> AccessTheWebAsync()
        { 
            HttpClient client = new HttpClient();
            Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");
            DoIndependentWork();
            string urlContents = await getStringTask;

            return urlContents.Length;
        }


        void DoIndependentWork()
        {
            resultsTextBox.Text += "Working . . . . . . .\r\n";
        }

  1. When AccessTheWebAsync is called then await keyword is used, what does it mean?
  2. When this function AccessTheWebAsync() will be executing then DoIndependentWork() function is called and i guess here control will be waiting until this function DoIndependentWork() is finished. Am I right?

again there is another statement called

string urlContents = await getStringTask;

why they use here await. if we do not use await here then what would happen?

Please guide me to understand the code that how it is working.

解决方案

Here's a brief description about async/await methods.

Async Methods:

  • Caller is not necessarily blocked for the full execution of async method
  • Possible return types
    • void: "fire-and-forget"
    • Task: allows to await termination of async method
    • Task<T>: allows to await termination and get result of type T
  • No ref or out parameter for async methods
  • Must again contain an await => Otherwise compile warning

await for Tasks

  • Await termination of a TPL task
  • Return result of task (if task with result type)
  • Must only occur in async methods => Otherwise compile error

An async method is partly synchronous and partly asynchronous

  • Caller synchronously executes the method until a blocking await
  • Thereafter, the method rest is asynchronously executed async & await Mechanism Efficient

public async Task<int> GetSiteLengthAsync(string url) 
{  
    HttpClient client = new HttpClient();               <= Sync  
    Task<string> download1 = client.GetStringAsync(url); <= Sync  
    string site1 = await download1;   <= Async (Another thread)
    return site1.Length;              <= Async (Another thread)
}

这篇关于寻找指导,以了解与异步如何异步编程并等待作品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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