[英]How do I get my C# Async and Wait code to work?
有人可以帮我这个吗,这是我的代码,我想让解压缩任务等待文件下载,但是它不是一个,并且它不让我等待一个空白,我到处都看了看,所以我无法弄清楚有人可以给我发回工作代码吗(请注意,所有这些代码都可以工作,但不能与异步代码一起使用):
WebClient client = new WebClient();
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileAsync(new Uri("https://webserver-test-1.000webhostapp.com/spacelightzipped.zip"), Environment.CurrentDirectory + "\\spacelightzipped.zip");
String ZipPath = Environment.CurrentDirectory + "\\spacelightzipped.zip";
String extractPath = Environment.CurrentDirectory;
ZipFile.ExtractToDirectory(ZipPath, extractPath);
System.Diagnostics.Process proc = new System.Diagnostics.Process
{
EnableRaisingEvents = false
};
proc.StartInfo.FileName = Environment.CurrentDirectory + "\\SpaceLightApp.exe";
proc.Start();
您必须使用事件WebClient.DownloadFileCompleted事件,该事件将在文件完全下载后引发,然后对文件执行所需的任何代码。
因此,您需要为您的webClient注册事件。 喜欢 :
client.DownloadFileCompleted += wc_DownloadFileCompleted;
然后调用您的代码以提取并在DownloadFileCompleted事件中执行。
private static void wc_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
String ZipPath = Environment.CurrentDirectory + "\\spacelightzipped.zip";
String extractPath = Environment.CurrentDirectory;
ZipFile.ExtractToDirectory(ZipPath, extractPath);
System.Diagnostics.Process proc = new System.Diagnostics.Process
{
EnableRaisingEvents = false
};
proc.StartInfo.FileName = Environment.CurrentDirectory + "\\SpaceLightApp.exe";
proc.Start();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.