Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Don';t在正在等待的异步方法内等待异步调用_C#_Multithreading_Xaml_Asynchronous_Win Universal App - Fatal编程技术网

C# Don';t在正在等待的异步方法内等待异步调用

C# Don';t在正在等待的异步方法内等待异步调用,c#,multithreading,xaml,asynchronous,win-universal-app,C#,Multithreading,Xaml,Asynchronous,Win Universal App,我从异步方法中看到一些奇怪的行为。我最近发现,并非所有Windows设备都能解压我的整个zip存档。如此之多以至于我求助于提取我需要的单个文件,在我等待提取其余归档文件时使用它。但是,目前提取单个文件的代码和提取整个归档文件的代码都是从同一个方法调用的。此方法是异步的,最初由App.xaml.cs中的代码在UI线程上调用。当我调用这个方法时,我使用wait关键字等待它完成,因为zip存档中有一个文件需要加载应用程序 App.xaml如下所示: SharedContext.ChangeUniver

我从异步方法中看到一些奇怪的行为。我最近发现,并非所有Windows设备都能解压我的整个zip存档。如此之多以至于我求助于提取我需要的单个文件,在我等待提取其余归档文件时使用它。但是,目前提取单个文件的代码和提取整个归档文件的代码都是从同一个方法调用的。此方法是异步的,最初由App.xaml.cs中的代码在UI线程上调用。当我调用这个方法时,我使用wait关键字等待它完成,因为zip存档中有一个文件需要加载应用程序

App.xaml如下所示:

SharedContext.ChangeUniverse("1234");
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
}
SharedContext如下所示:

SharedContext.ChangeUniverse("1234");
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
}

问题是,下载存档在ExtractFullArchive完成之前不会返回,而ExtractFullArchive需要很长时间。我需要ExtractFullArchive在DownloadArchive完成时异步执行。我真的不在乎它什么时候结束。

如果你不想等待,就不要返回任务,而是像这样返回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
}

如果您不在乎
ExtractFullArchive
何时完成,您可以启动一个新的
任务
在另一个线程上执行该方法。使用这种方法,
downloadchive
方法完成,尽管
ExtractFullArchive
尚未完成。例如,这可能看起来像这样

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));
}

extractodirectory
实际上是一个异步方法吗?Async void应该只用于事件处理程序之类的东西,这似乎是错误的。