Http web request freezing
I can do this once and it’s fine. However, if I call it again after the download has completed and exited, the process times out on the wcdownload.openRead() command in the download() thread.
I can’t figure out why it freezes like that.
Here’s the code for the download form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading;
namespace DDDD_Update
{
/// <summary>
/// Just a simple progress bar downloader for
/// getting the updates from a web address.
/// The calling form creates a new instance
/// of this class/form and then shows the form.
/// Then the calling form must call the
/// public function "initializeDownload"
/// which will start downloading whatever the
/// the file is.
/// </summary>
///
public partial class frmDownload : Form
{
private Thread thrDownload;
private Stream strResponse;
private Stream strLocal; //local data stream
private HttpWebRequest webRequest;
private HttpWebResponse webResponse;
private int PercentProgress;
private string theWebAddress;
private string destinationFile;
private string downloadStatus;
private delegate void UpdateProgessCallback(Int64 BytesRead, Int64 TotalBytes);
private delegate void completeCallback();
public frmDownload()
{
InitializeComponent();
}
private void frmDownload_Load(object sender, EventArgs e)
{
downloadStatus = globals.DL_NEUTRAL;
}
public string initializeDownload(string webAddy, string fileLocation)
{
theWebAddress = webAddy;
destinationFile = fileLocation;
thrDownload = new Thread(download);
thrDownload.Start();
while (downloadStatus == globals.DL_NEUTRAL)
{
System.Threading.Thread.Sleep(20);
Application.DoEvents();
}
System.Threading.Thread.Sleep(100);
return downloadStatus ;
}
private void download()
{
WebClient wcDownload = new WebClient();
try
{
//make request to get file info
System.Diagnostics.Debug.Print("d1");
webRequest = (HttpWebRequest)WebRequest.Create(theWebAddress);
webRequest.Proxy = null;
System.Diagnostics.Debug.Print("d2");
webRequest.Credentials = CredentialCache.DefaultCredentials;
System.Diagnostics.Debug.Print("d3");
webResponse = (HttpWebResponse)webRequest.GetResponse();
System.Diagnostics.Debug.Print("d4");
Int64 fileSize = webResponse.ContentLength;
// download it
wcDownload.Proxy = null;
System.Diagnostics.Debug.Print("d5");
System.Diagnostics.Debug.Print(theWebAddress);
strResponse = wcDownload.OpenRead(theWebAddress);
System.Diagnostics.Debug.Print("d6");
strLocal = new FileStream(destinationFile, FileMode.Create, FileAccess.Write, FileShare.None);
System.Diagnostics.Debug.Print("d7");
int bytesSize = 0;
byte[] downBuffer = new byte[2048];
// Loop through the buffer until it's empty
int count = 0;
while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
strLocal.Write(downBuffer, 0, bytesSize);
//update progress bar
this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { strLocal.Length, fileSize });
count++;
System.Diagnostics.Debug.Print(count.ToString());
}
}
finally
{
strResponse.Close();
System.Diagnostics.Debug.Print("strReponse Closed");
strLocal.Close();
System.Diagnostics.Debug.Print("strLocal Closed");
downloadStatus = globals.DL_SUCCESS;
}
}
private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)
{
PercentProgress = Convert.ToInt32((BytesRead * 100) / TotalBytes);
progBar1.Value = PercentProgress;
lblSize.Text = "Downloaded " + BytesRead + " out of " + TotalBytes + " (" + PercentProgress + "%)";
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
26