Sunday, December 28, 2014

C#: looping threads

hello


I use this link:


http://www.geekpedia.com/tutorial179_Creating-a-download-manager-in-Csharp.html


I want my program to read "address file" and download URLs line by line.


I edit code thus, but my program can not read next line of "address file".



private void Download()
{
using (WebClient wcDownload = new WebClient())
{
try
{
String FilePath_address = Application.StartupPath + @"\address.txt";
string line = "";
// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader(FilePath_address);
while ((line = file.ReadLine()) != null)
{
if (txtUrl.InvokeRequired)
{
MessageBox.Show("1");
txtUrl.Invoke(new Action(() => txtUrl.Text = line));
}
else
{
txtUrl.Text = line;
}
if (txtPath.InvokeRequired)
{
MessageBox.Show("2");
txtPath.Invoke(new Action(() => txtPath.Text = Application.StartupPath + @"\D1.pdf"));
}
else
{
txtPath.Text = Application.StartupPath + @"\D1.pdf";
}
// Create a request to the file we are downloading
webRequest = (HttpWebRequest)WebRequest.Create(txtUrl.Text);
// Set default authentication for retrieving the file
webRequest.Credentials = CredentialCache.DefaultCredentials;
// Retrieve the response from the server
webResponse = (HttpWebResponse)webRequest.GetResponse();
// Ask the server for the file size and store it
Int64 fileSize = webResponse.ContentLength;
// Open the URL for download
strResponse = wcDownload.OpenRead(txtUrl.Text);
// Create a new file stream where we will be saving the data (local drive)
strLocal = new FileStream(txtPath.Text, FileMode.Create, FileAccess.Write, FileShare.None);
// It will store the current number of bytes we retrieved from the server
int bytesSize = 0;
// A buffer for storing and writing the data retrieved from the server
byte[] downBuffer = new byte[2048];
// Loop through the buffer until the buffer is empty
while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
// Write the data from the buffer to the local hard drive
strLocal.Write(downBuffer, 0, bytesSize);
// Invoke the method that updates the form's label and progress bar
this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { strLocal.Length, fileSize });
}
if (prgDownload.InvokeRequired)
{
prgDownload.Invoke(new Action(() => prgDownload.Value = 0));
}
else
{
prgDownload.Value = 0;
}
if (lblProgress.InvokeRequired)
{
lblProgress.Invoke(new Action(() => lblProgress.Text = "Next Download Starting"));
}
else
{
lblProgress.Text = "Next Download Starting";
}
// Close the web response and the streams
//webResponse.Close();
//strResponse.Close();
//strLocal.Close();
// Abort the thread that's downloading
//thrDownload.Abort();
// Create a new thread that calls the Download() method
thrDownload = new Thread(Download);
// Start the thread, and thus call Download()
thrDownload.Start();
}
}
finally
{
// When the above code has ended, close the streams
strResponse.Close();
strLocal.Close();

}
}
}


No comments:

Post a Comment