Hi!
I'm trying to write a Downloader that downloads in that case 3 files.
Downloading is done in a thread while the main thread should print the
status.
Unfortunatly, if I have 3 files that should be downloaded the last Thread
never terminates. Whereas, I download 1 or 2 files everything works fine.
I tested it with ThreadPools and Async Threads, but it's always the same
result.
I can't see any sense in that behavior. I would be more than happy if
someboy could help me.
--
Greetings,
Sebastian Pölsterl
using System;
using System.Net;
using System.IO;
using System.Threading;
public delegate void StatusHandler(string action, double progress);
public class Downloader
{
private string download_url;
private string download_dest;
private Stream strResponse;
private Stream strLocal;
public event StatusHandler Status;
public Downloader(string download_url, string download_dest)
{
Console.WriteLine("Creating Downloader");
this.download_url = download_url;
this.download_dest = download_dest;
}
public void Run() {
string filename = Path.GetFileName(this.download_url);
string localFile = Path.Combine(this.download_dest, filename);
using(WebClient wc = new WebClient()) {
try {
// Create a request to the file we are downloading
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(this.download_url);
// Set default authentication for retrieving the file
webRequest.Credentials = CredentialCache.DefaultCredentials;
// Retrieve the response from the server
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
// Ask the server for the file size and store it
long fileSize = webResponse.ContentLength;
// Open the URL for download
strResponse = wc.OpenRead(this.download_url);
// Create a new file stream where we will be saving the data (local drive)
strLocal = new FileStream(localFile, 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.OnStatus( String.Format("Downloading {0}", filename), (double)strLocal.Length/fileSize );
}
}
finally {
// When the above code has ended, close the streams
strResponse.Close();
strLocal.Close();
}
}
}
private void OnStatus(string action, double progress) {
if (Status != null) {
Status(action, progress);
}
}
}
public class Test
{
public delegate void DownloaderDel();
private static void DownloaderFinishedCallback(IAsyncResult ar) {
Console.WriteLine("Update complete for {0}", (string)ar.AsyncState);
}
public static void Main (string[] args)
{
string[] files = new string[] {"http://raphael.slinckx.net/deskbar/repository/yahoo.py", "http://www.k-d-w.org/clipboard/deskbar/leoorg.tar.bz2", "http://raphael.slinckx.net/deskbar/repository/gmail-deskbar-hack.tar.bz2"};
string install_path = "/home/sebastian/Projects";
foreach(string url in files)
{
Downloader dl = new Downloader(url, install_path);
dl.Status += new StatusHandler(
delegate(string action, double progress) { Console.WriteLine("{0} {1}", action, progress); }
);
Thread dlThread = new Thread( new ThreadStart(dl.Run) );
dlThread.Start();
while (dlThread.IsAlive)
{
Console.Write(".");
Thread.Sleep(10);
}
/*
DownloaderDel dlDel = new DownloaderDel(dl.Run);
AsyncCallback dlCallback = new AsyncCallback(DownloaderFinishedCallback);
dlDel.BeginInvoke(dlCallback, url);
*/
}
}
}_______________________________________________
Mono-list maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-list