[英]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.