Thread[] download;
ThreadStart start = new System.Threading.ThreadStart(uploadimg);
download = new Thread[5];
for (int i = 0; i < 5; i++)
{
download[i] = new System.Threading.Thread(start);
download[i].Start();
}
static void Main(string[] args)
{
var tasks = Enumerable.Range(0, 50);
var q = from x in tasks.AsParallel().WithDegreeOfParallelism(5)
select DoIt(x);
Console.WriteLine("总共用了{0}个线程执行了{1}个任务!\n", q.Distinct().Count(), tasks.Count());
}
static int DoIt(int n)
{
var tid = Thread.CurrentThread.ManagedThreadId;
Console.WriteLine("{0} : {1}", tid, n);
return tid;
}
如果要用多线程去抓
首先你得取得所有图片链接列表
然后根据线程数量去为每一个线程分配好
再运行那些线程,最好不要线程运行的时候再去取全部图片列表,那样还得加互斥,影响效率
private Queuem_que_url = new Queue ();
private void DownLoadCallBack() {
string strUrl = string.Empty;
WebClient web = new WebClient();
while (true) {
lock (m_que_url) {
if (m_que_url.Count == 0) break;
strUrl = m_que_url.Dequeue();
}
try {
web.DownloadFile(strUrl, "yourfilename");
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
}
}
//set m_que_url
int nThreadCount = 50;
for (int i = 0; i < nThreadCount; i++) {
new Thread(DownLoadCallBack) { IsBackground = true }.Start();
}
先mark,节后我回来收取代码 刚好有上百万张图片url要抓,虽然大部分不怎样,但有小部分是很给力的图片 方便的话贴段代码,感谢楼主
private Queuem_que_url = new Queue ();
private void DownLoadCallBack() {
string strUrl = string.Empty;
WebClient web = new WebClient();
while (true) {
lock (m_que_url) {
if (m_que_url.Count == 0) break;
strUrl = m_que_url.Dequeue();
}
try {
web.DownloadFile(strUrl, "yourfilename");
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
}
}
//set m_que_url
int nThreadCount = 50;
for (int i = 0; i < nThreadCount; i++) {
new Thread(DownLoadCallBack) { IsBackground = true }.Start();
}
private Queuem_que_url = new Queue ();
private void DownLoadCallBack() {
string strUrl = string.Empty;
WebClient web = new WebClient();
while (true) {
lock (m_que_url) {
if (m_que_url.Count == 0) break;
strUrl = m_que_url.Dequeue();
}
try {
web.DownloadFile(strUrl, "yourfilename");
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
}
}
//set m_que_url
int nThreadCount = 50;
for (int i = 0; i < nThreadCount; i++) {
new Thread(DownLoadCallBack) { IsBackground = true }.Start();
}
兄台你好眼熟啊 --
private Queuem_que_url = new Queue ();
private void DownLoadCallBack() {
string strUrl = string.Empty;
WebClient web = new WebClient();
while (true) {
lock (m_que_url) {
if (m_que_url.Count == 0) break;
strUrl = m_que_url.Dequeue();
}
try {
web.DownloadFile(strUrl, "yourfilename");
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
}
}
//set m_que_url
int nThreadCount = 50;
for (int i = 0; i < nThreadCount; i++) {
new Thread(DownLoadCallBack) { IsBackground = true }.Start();
}
兄台你好眼熟啊 --
private Queuem_que_url = new Queue ();
private void DownLoadCallBack() {
string strUrl = string.Empty;
WebClient web = new WebClient();
while (true) {
lock (m_que_url) {
if (m_que_url.Count == 0) break;
strUrl = m_que_url.Dequeue();
}
try {
web.DownloadFile(strUrl, "yourfilename");
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
}
}
//set m_que_url
int nThreadCount = 50;
for (int i = 0; i < nThreadCount; i++) {
new Thread(DownLoadCallBack) { IsBackground = true }.Start();
}
是不是在你梦里出现过。。
我觉得异步会快些吧
我又回来了!!
我又回来了!!
怎么给代码你 ,没看到传文件的呀
我又回来了!!
怎么给代码你 ,没看到传文件的呀
哟西,非常感谢~~
把代码直接张贴上来就OK啦。
如果是demo,包含解决方案,引用库之类的,可以上传到CSDN下载,让大家可以下载,然后张贴下载链接到帖子后面。
讨论一个问题,最后得到一些解决办法,作为总结放到帖子最后,然后结贴。论坛本来就应该这样的
若图片平均大小为 1M,那么 4000 / (15 * 60) = 4.44 M/s
也就是你这少需要 30M 线路带宽,已经很快了
如果开宽还有富裕,是可以考虑用多线程的
你可以设一个待抓取图片的队列,每个线程只从队首取出图片 url,这样就不会重复了
如果抓取失败,还应将相应 url 重新入队