简体   繁体   中英

How to wait all asynchronous operations?

I use recursive function to do something

public async void walk(StorageFolder folder)
{
   IReadOnlyList<StorageFolder> subDirs = null;
   subDirs = await folder.GetFoldersAsync();
   foreach (var subDir in subDirs)
   {
      var dirPath = new Profile() { FolderPath = subDir.Path};
      db.Insert(dirPath);
      walk(subDir);
   }
   tbOut.Text = "Done!";
}

So, I want that tbOut.Text = "Done!"; will be done only after all iterations ends. At now it's happenning at the same time while iterations under process. If I run this function like that

walk(fd);
tbOut.Text = "Done!";

the result still the same. How to wait when this function will ends completely?

You don't await the completion of your sub-calls to your walk function. So all you have to do is change it to await walk(subDir) . However, since you can't await a void function you'll have to change it a bit to make it work. To make your walk function awaitable change the return type to Task like so:

public async Task walk(StorageFolder folder)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM