c# - Multiple Awaits in a single method -


i have method 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 follow happen when calls saveallasync().

what think happen this:

  1. when first calls it, saveallasync() return control caller @ line await xmlwriter.writestartdocumentasync();
  2. then... when await saveallasync() (or wait task)... happens? saveallasync() still stuck on first await until called? because there's no threading involved, guess case...

you can think of await "pausing" async method until operation complete. special case, if operation completed (or extremely fast), await not "pause" method; continue executing immediately.

so in case (assuming writestartdocumentasync not completed), await pause method , return uncompleted task caller. note task returned async method represents method; when method completes, task completed.

eventually, writestartdocumentasync complete, , schedule rest of async method continue running. in case, it'll execute next part of method until next await, when gets paused again, etc. eventually, async method complete, complete task returned represent method.

for more information, have async/await intro on blog.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -