我正在使用 Mono 开发一个可以同时下载多个文件的程序(适用于 Mac OS X 和 Debian)。
然而,尽管我使用了构造函数,但我只能同时下载 2 个文件new RollingDownload(10)
。我正在使用的代码是这样的
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading;
using System.Net;
using System.Diagnostics;
using System.IO;
namespace worker
{
public class RollingDownload
{
private static ConcurrentQueue<Download> _downloads = new ConcurrentQueue<Download>();
private static short _NumberOfThreads;
private static short DefaultTimeoutSeconds = 20;
public RollingDownload (short NumberOfThreads)
{
_NumberOfThreads = NumberOfThreads;
}
public void addDownload(Download download) {
_downloads.Enqueue(download);
}
public void SpawnWebRequests()
{
ServicePointManager.DefaultConnectionLimit = _NumberOfThreads;
ServicePointManager.MaxServicePoints = _NumberOfThreads;
IList<Thread> threadList = new List<Thread>();
for (int i = 0; i < _NumberOfThreads; i++)
{
var thread = new Thread(ProcessWebRequests);
threadList.Add(thread);
thread.Start();
}
for (int i = 0; i < _NumberOfThreads; i++)
{
threadList[i].Join();
}
}
private static void ProcessWebRequests()
{
while (true)
{
Download download;
Console.WriteLine (Thread.CurrentThread.ManagedThreadId);
if(_downloads.TryDequeue(out download)) {
ProcessWebRequest(download);
} else {
break;
}
}
}
private static void ProcessWebRequest(Download download)
{
try
{
var request = (HttpWebRequest)WebRequest.Create(download.Url);
request.Method = "GET"; // or "GET", since some sites (Amazon) don't allow HEAD
request.Timeout = DefaultTimeoutSeconds * 1000;
request.AllowAutoRedirect = true;
//request.ServicePoint.ConnectionLimit = _NumberOfThreads;
request.KeepAlive = false;
//request.ServicePoint = ServicePointManager.FindServicePoint(new Uri(download.Url));
// Get the response (throws an exception if status != 200)
using (var response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode == HttpStatusCode.OK) {
/*string contents = "";
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
contents = reader.ReadToEnd();
}*/
download.onCompleted(response.GetResponseStream(), response.StatusCode);
}
}
}
catch (WebException ex)
{
var response = ((HttpWebResponse)ex.Response);
var status = response != null
? response.StatusCode
: HttpStatusCode.RequestTimeout;
Console.WriteLine(String.Format("Broken link ({0}): {1}", status, download.Url), ex);
// Don't rethrow, as this is an expected exception in many cases
}
catch (Exception ex)
{
Console.WriteLine(String.Format("Error processing link {0}", download.Url), ex);
// Rethrow, something went wrong
throw;
}
}
}
public class Download
{
public string Url { get; set; }
public string PathToSave { get; set; }
public Download (String Url)
{
this.Url = Url;
}
public void onCompleted (Stream response, HttpStatusCode httpcode)
{
Console.WriteLine ("hello everybody: " + httpcode.ToString ());
}
}
}
好吧,我不知道。 #mono IRC 频道中的某人的意思是,我应该使用这张票 http://support.microsoft.com/default.aspx?scid=kb;en-us;821268#XSLTH3158121122120121120120解决问题,但我不知道在哪里可以找到 machine.config 或如何将其添加到 monodevelop 中。
我正在开发的应用程序是使用 C# 的控制台应用程序(无 ASP!)。
很高兴收到你们的来信。
你们的下载是从同一个主机上下载的吗?如果是,您需要向构造函数添加一些代码RollingDownload
(或其他初始化代码):
string downloadHost = ...;
ServicePoint sp = ServicePointManager.FindServicePoint(new Uri(downloadHost));
sp.ConnectionLimit = _NumberOfThreads;
[归功于这个博客 http://palblog.fxpal.com/?p=3469这帮助我之前解决了类似的问题。]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)