异步方法不会继续执行代码
本文关键字:执行 代码 继续 异步方法 | 更新日期: 2023-09-27 18:09:02
我想创建一些嵌套文件夹的对象,我发送如果有父id当我调试它等待后,它的行为就像打破它不跟随下一个代码行我能做什么我找不到任何解决方案在互联网上
public class FileOperations
{
StorageFolder newFile,newFolder;
List<StorageFolder> folderList = new List<StorageFolder>();
public async void CreatingFiles(int cat, int par, string name)
{
StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder;
if (par == 0)
{
StorageFolder file = Windows.Storage.ApplicationData.Current.LocalFolder;
newFile = await file.CreateFolderAsync(name, CreationCollisionOption.ReplaceExisting);
folderList.Add(newFile);
}
else
{
for (int i = 1; i <= folderList.Count; i++)
if (par == i)
{
newFolder = await folderList[par - 1].CreateFolderAsync(name, CreationCollisionOption.ReplaceExisting);
folderList.Add(newFolder);
}
}
}
public Task InitializeAsync()
{
return InitializeOfflineFilesAsync();
}
private Task InitializeOfflineFilesAsync()
{
throw new NotImplementedException();
}
private async Task<StorageFolder> CF(string name, CreationCollisionOption replaceExisting, StorageFolder file)
{
return await file.CreateFolderAsync(name, replaceExisting);
}
}
您是否尝试过返回任务类型而不是为"CreatingFiles"方法无效并调用"CreatingFiles"方法等待?
。等待CreatingFiles(…),
我猜你调用async方法几次,你不希望其他async方法开始前一个async方法完成,但它确实。为了防止这种情况,你必须调用"CreatingFiles"方法,在它继续实现之前等待它完成。
希望能有所帮助。
异步是指不依赖于彼此结果的进程,因此可以同时发生在不同的线程上。所以我认为问题是你跑了三分之一,然后你切换到不同的三分之一,你必须在相同的三分之一,如果你想继续。解决方案是使用分配器联机代码,该代码中断:
App.Current.Dispatcher.Invoke(async () =>
{
var x = await ...;
});