简体   繁体   English

不要等待正在等待的异步方法中的异步调用

[英]Don't wait for Async call inside async method that is being waited on

I am seeing some odd behavior from my async methods. 我从异步方法中看到了一些奇怪的行为。 I recently discovered that unzipping my entire zip archive was not performant on all Windows devices. 我最近发现,在所有Windows设备上都无法对整个zip存档进行解压缩。 So much so that I've resorted to extracting a single file I need, using it while I wait for the rest of the archive to extract. 如此之多,以至于我不得不提取我需要的单个文件,并在等待存档的其余部分提取时使用它。 However, currently the code to extract the single file and the code to extract the entire archive is being called from the same method. 但是,当前从同一方法调用提取单个文件的代码和提取整个档案的代码。 This method is async and ultimately originally called on the UI thread by code in App.xaml.cs. 该方法是异步的,最终最初由App.xaml.cs中的代码在UI线程上调用。 When I call this method, I am using the await keyword to wait for it to complete as there is one file in the zip archive I need for the loading of the app. 当我调用此方法时,我正在使用await关键字来等待它完成,因为zip档案中有一个文件需要加载该应用程序。

App.xaml looks like this: App.xaml看起来像这样:

SharedContext.ChangeUniverse("1234");

SharedContext looks like this: SharedContext看起来像这样:

public static void ChangeUniverse(string universe) {
    await DownloadArchive(universe);
}

public async Task DownloadArchive(string universe) {
    ZipArchive archive = magic; // get it somehow
    var someLocalFilePath = magic; // the exact location I need to extract data.json
    var someLocalPath = magic; // the exact location I need to extract the zip
    archive.GetEntry("data.json").ExtractToFile(someLocalFilePath);
    // notice I do NOT await
    ExtractFullArchive(archive, someLocalPath);
}

public async Task ExtractFullArchive(ZipArchive archive, string path) {
    archive.ExtractToDirectory(path, true); // extracting using an override nice extension method I found on SO.com
}

The problem is that DownloadArchive doesn't return until ExtractFullArchive completes and ExtractFullArchive is what is taking a LONG time. 问题在于,直到ExtractFullArchive完成并且ExtractFullArchive花了很长时间之后,DownloadArchive才返回。 I need ExtractFullArchive to execute asynchronously while DownloadArchive completes. 我需要ExtractFullArchive在DownloadArchive完成时异步执行。 I really don't care when it finishes. 我真的不在乎什么时候完成。

Dont return Task if you don't want to wait instead return void like this 如果您不想等待,请不要返回Task,而是返回void

public async void ExtractFullArchive(ZipArchive archive, string path) {
    archive.ExtractToDirectory(path, true); // extracting using an override nice extension method I found on SO.com
}

When you don't care when ExtractFullArchive finishes, you can start a new Task to execute the method on another thread. 当您不在乎ExtractFullArchive何时完成时,可以启动一个新的Task来在另一个线程上执行该方法。 With this approach, the DownloadArchive method finishes, although the ExtractFullArchive has not finished yet. 使用此方法,尽管ExtractFullArchive尚未完成,但DownloadArchive方法完成了。 This could look like this for example. 例如,这可能看起来像这样。

public async Task DownloadArchive(string universe) {
    ZipArchive archive = magic; // get it somehow
    var someLocalFilePath = magic; // the exact location I need to extract data.json
    var someLocalPath = magic; // the exact location I need to extract the zip
    archive.GetEntry("data.json").ExtractToFile(someLocalFilePath);
    Task.Run(() => ExtractFullArchive(archive, someLocalPath));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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