On 12/26/06, Robert Jordan <[EMAIL PROTECTED]> wrote:
Sebastian Pölsterl wrote:
> 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.
According to the HTTP RFC, a client must not maintain more than
2 open connections to the server. WebClient is respecting this
restriction.
Maybe he has another problem. He's creating a WebRequest/WebResponse
pair and is never closing it. In fact, he's not even using it, he's
just taking the file size. So for each file it creates two connections
to the same server, being one not closed in the end, so in practice
(with his code) only one download per server can be done concurrently.
Am I right?
Try not using WebClient (as it's just sugar on WebRequest), see the
attached file (which works here).
Cheers,
--
Felipe.
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;
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);
// 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
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()) {
// Ask the server for the file size and store it
long fileSize = webResponse.ContentLength;
// Open the URL for download
Stream strResponse = webResponse.GetResponseStream();
// Create a new file stream where we will be saving the data (local drive)
Stream 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 );
}
}
}
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 = "";
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();
/*
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