在一个单一的方法多等待 [英] Multiple Awaits in a single method

查看:154
本文介绍了在一个单一的方法多等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个方法:

 公共静态异步任务SaveAllAsync(){
        的foreach(在configurationFileMap VAR KVP){
            使用(的XmlWriter的XmlWriter = XmlWriter.Create(kvp.Value,XML_WRITER_SETTINGS)){
                字段信息[] = allPublicFields kvp.Key.GetFields(BindingFlags.Public | BindingFlags.Static);
                等待xmlWriter.WriteStartDocumentAsync();
                的foreach(字段信息网络allPublicFields){
                    等待xmlWriter.WriteStartElementAsync(一些,文本,这里);
                }
                等待xmlWriter.WriteEndDocumentAsync();
            }
        }
    }

但我在努力跟随当有人来电 SaveAllAsync会发生什么()

我觉得会发生是这样的:


  1. 当有人首先调用它, SaveAllAsync()将在该行控制权返回给调用者等待xmlWriter.WriteStartDocumentAsync();

  2. 然后...当他们在等待 SaveAllAsync()(或者等待任务)......会发生什么?将 SaveAllAsync()仍是第一次的await卡住,直到那个叫?因为没有涉及线程,我想是这样的话...


解决方案

您可以把的等待为暂停的异步方法,直到操作完成。作为一个特殊的情况下,如果操作已经完成(或者是的非常的快),那么等待不会暂停的方法;它会继续立即执行。

因此​​,在这种情况下(假设 WriteStartDocumentAsync 尚未完成),等待将暂停的方法和回报未完成的任务给调用者。请注意,工作异步方法重新presents该方法返回;该方法完成时,那么工作完成。

最后, WriteStartDocumentAsync 将完成,这将安排异步方法继续运行的其余部分。在这种情况下,它会执行该方法的下一部分,直到下一个等待,当它得到又停了下来,等最后,异步方法将完成,这将完成工作是返回到重新present该方法。

有关详细信息,我有一个<一个href=\"http://blog.stephencleary.com/2012/02/async-and-await.html\"><$c$c>async/<$c$c>await引导页上我的博客。

I have a method like this:

public static async Task SaveAllAsync() {
        foreach (var kvp in configurationFileMap) {
            using (XmlWriter xmlWriter = XmlWriter.Create(kvp.Value, XML_WRITER_SETTINGS)) {
                FieldInfo[] allPublicFields = kvp.Key.GetFields(BindingFlags.Public | BindingFlags.Static);
                await xmlWriter.WriteStartDocumentAsync();
                foreach (FieldInfo fi in allPublicFields) {
                    await xmlWriter.WriteStartElementAsync("some", "text", "here");
                }
                await xmlWriter.WriteEndDocumentAsync();
            }
        }
    }

But I'm struggling to follow what will happen when someone calls SaveAllAsync().

What I think will happen is this:

  1. When someone first calls it, SaveAllAsync() will return control to the caller at the line await xmlWriter.WriteStartDocumentAsync();
  2. Then... When they await SaveAllAsync() (or wait for the task)... What happens? Will SaveAllAsync() still be stuck on the first await until that is called? Because there's no threading involved, I guess that is the case...

解决方案

You can think of await as "pausing" the async method until that operation is complete. As a special case, if the operation is already completed (or is extremely fast), then the await will not "pause" the method; it will continue executing immediately.

So in this case (assuming that WriteStartDocumentAsync is not already completed), await will pause the method and return an uncompleted task to the caller. Note that the Task returned by an async method represents that method; when the method completes, then that Task is completed.

Eventually, WriteStartDocumentAsync will complete, and that will schedule the rest of the async method to continue running. In this case, it'll execute the next part of the method until the next await, when it gets paused again, etc. Eventually, the async method will complete, which will complete the Task that was returned to represent that method.

For more information, I have an async/await intro on my blog.

这篇关于在一个单一的方法多等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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