问题
I'm facing another problem, my application downloads a file from a web, extracts it, deletes it and so on, it runs fine for the first run, then when it comes to downloading the next file it simply freezes the download and hangs there forever.. It's probably something with trying to open an already open connection, but I have no idea how to close it, this is my first time networking with C# and I'm self teaching.
My code:
public void start() {
if (File.Exists("Data/version.txt")) { File.Delete("Data/version.txt"); }
label1.Text = "Getting update information...";
WebClient webClient = new WebClient();
webClient.DownloadFileAsync(new Uri("http://127.0.0.1/update/version.txt"), @"Data/version.txt");
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(versionCompleted);
}
private void versioncheck() {
if (File.Exists("Main.exe"))
{
label1.Text = "Contacting update server...";
var versionInfo = FileVersionInfo.GetVersionInfo("Main.exe");
string version = versionInfo.ProductVersion;
string[] nversion = version.Split('.');
string updateversion = nversion[3];
int version = Int32.Parse(updateversion);
///////////////////////////////////////////////////////////
StreamReader sr = new StreamReader("Data/version.txt");
///////////////////////////////////////////////////////////
string readline = sr.ReadLine();
sr.Dispose();
int serverversion = Int32.Parse(readline);
if (serverversion > version) {
string filenumber = (version+1).ToString();
downloadfile(filenumber);
}
else if(serverversion == version){
label1.Text = "Game is up to date!";
startButton.Enabled = true;
}
}
else { MessageBox.Show("Unexpected Error!", "Error!"); }
}
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
label1.Text = "Extracting Files...";
var versionInfo = FileVersionInfo.GetVersionInfo("Main.exe");
string version = versionInfo.ProductVersion;
string[] nversion = version.Split('.');
string updateversion = nversion[3];
int version = Int32.Parse(updateversion); string nversion = (version + 1).ToString();
Process proc = Process.Start("update"+nversion+".exe"); // extract in the silent mode
proc.WaitForExit();
File.Delete("update" + nversion + ".exe");
label1.Text = "Checking for more updates...";
versioncheck();
}
private void versionCompleted(object sender, AsyncCompletedEventArgs e) {
versioncheck();
}
private void downloadfile(string filenumber)
{
try
{
//MessageBox.Show("Download working");
System.Net.WebClient webClient = new System.Net.WebClient();
webClient.OpenRead("http://127.0.0.1/Update/update" + filenumber + ".exe");
Int64 bytes_total = Convert.ToInt64(webClient.ResponseHeaders["Content-Length"]);
string updatelength = Convert.ToString((bytes_total / 1024).ToString());
label2.Text = "File size:" + updatelength + "KB";
////////////////////////////////////////////////////
label1.Text = "Downloading Update...";
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync(new Uri("http://127.0.0.1/Update/update" + filenumber + ".exe"), @"update"+filenumber+".exe");
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.Dispose();
}
catch (WebException )
{
MessageBox.Show("Connection error!", "Error!");
Application.Exit();
}
catch (IOException)
{
MessageBox.Show("Unknown Error!", "Error!");
Application.Exit() ;
}
}
回答1:
If u use async operation u gonne need to use the async and await keywords.
U need to dispose or close your webclient when it is done downloading or failing.
As b1tsh1ft stated best thing is to use the using statement.
string version = versionInfo.ProductVersion;
string[] nversion = version.Split('.');
int version = Int32.Parse(updateversion);
string nversion = (version + 1).ToString();
Isn't your VS not giving a conflicting error in your editor about this ?
回答2:
First you should put WebClient in a using block because it implements IDisposable
using(var webClient = new WebClient())
{
// do work here
webClient.DownloadFile(..)
}
Don't use the async version. An exception is likely being thrown and is lost on the other thread. Test and make it work regularly first.
Also put your StreamReader (or anything implementing IDisposable) in a using() statement. It is more reliable then manually calling dispose because it disposes even on failure.
来源:https://stackoverflow.com/questions/21004406/webclient-freeze-after-download