C# 完成的异步下载将关闭两次,而不是一次
我的应用程序中有这段代码,允许用户下载应用程序的最新版本。应用程序下载完成后,如果用户希望打开文件位置以查看文件,则会打开一个提示 但是,该工具会启动两个消息框,而不是只启动一次。我不确定我是否遗漏了什么C# 完成的异步下载将关闭两次,而不是一次,c#,wpf,C#,Wpf,我的应用程序中有这段代码,允许用户下载应用程序的最新版本。应用程序下载完成后,如果用户希望打开文件位置以查看文件,则会打开一个提示 但是,该工具会启动两个消息框,而不是只启动一次。我不确定我是否遗漏了什么 private void BTN_GNV_MouseUp(object sender, MouseButtonEventArgs e) { string URLDir = "http://shard.combatkingz.com/downloads/"; string URL
private void BTN_GNV_MouseUp(object sender, MouseButtonEventArgs e)
{
string URLDir = "http://shard.combatkingz.com/downloads/";
string URLName = "DayZ Config Tweak tool v" + Properties.Settings.Default.AvailableVersion + ".exe";
string URLFull = "";
using (WebClient DLWC = new WebClient())
{
URLFull = URLDir + URLName;
GlobalVars.DLPath = System.Environment.CurrentDirectory + "\\" + URLName;
try
{
DLWC.DownloadFileAsync(new Uri(URLFull), GlobalVars.DLPath);
DLWC.DownloadProgressChanged += DLWC_DownloadProgressChanged;
}
catch
{
MessageBox.Show("There was an error downloading the file.", GlobalVars.APPNAME, MessageBoxButton.OK, MessageBoxImage.Error);
#if DEBUG
#else
AddDownloadToDB("Failed");
#endif
}
}
}
void DLWC_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
PB_GNV.Width = (BTN_GNV.Width / 100) * e.ProgressPercentage;
if (PB_GNV.Width == BTN_GNV.Width && e.TotalBytesToReceive == e.BytesReceived)
{
MessageBoxResult nav = MessageBox.Show("New version downloaded. Do you want to navigate to the folder?", GlobalVars.APPNAME, MessageBoxButton.YesNo, MessageBoxImage.Error);
if (nav == MessageBoxResult.Yes)
{
string argument = @"/select, " + @GlobalVars.DLPath;
System.Diagnostics.Process.Start("explorer.exe", argument);
#if DEBUG
#else
AddDownloadToDB("Success");
#endif
}
}
}
我怀疑在收到最后一个字节和文件完成时触发了
DownloadProgressChanged
事件。使用DownloadFileCompleted
事件应该可以解决问题 对。DownloadFileCompleted处理程序修复了该问题。它不再显示两次,并且工作完美。非常感谢。