Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 624
splash27 Avatar asked Oct 04 '22 05:10

splash27


1 Answers

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)
like image 146
shriek Avatar answered Oct 13 '22 10:10

shriek