5

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?

4

1 回答 1

4

您无需等待对 walk 函数的子调用完成。因此,您所要做的就是将其更改为await walk(subDir). 但是,由于您不能等待 void 函数,因此您必须对其进行一些更改才能使其正常工作。要使您的 walk 函数可等待,请将返回类型更改为Task

public async Task walk(StorageFolder folder)
于 2013-05-05T15:22:50.833 回答