/// <summary> /// do in a separate thread /// 下载缩略图线程 /// </summary> /// <param name="page"></param> /// <param name="count"></param> /// <param name="word"></param> public void PreFetchPage(int page, int count, string word, ImageSite site) { (new Thread(new ThreadStart(() => { try { preFetchedPage = site.GetPageString(page, count, word, proxy); prePage = page; preCount = count; preWord = word; preSite = site; List <Img> imgs = site.GetImages(preFetchedPage, proxy); //获得所有图片列表后反馈得到的数量 PreListLoaded(imgs.Count, null); if (imgs.Count < 1) { return; } SessionClient sweb = new SessionClient(); SessionHeadersCollection shc = new SessionHeadersCollection(); shc.Accept = null; shc.ContentType = SessionHeadersValue.ContentTypeAuto; shc.Add("Accept-Ranges", "bytes"); shc.Referer = site.Referer; imgs = site.FilterImg(imgs, MainWindow.MainW.MaskInt, MainWindow.MainW.MaskRes, MainWindow.MainW.LastViewed, MainWindow.MainW.MaskViewed, true, false); //预加载缩略图 foreach (HttpWebRequest req1 in imgReqs) { if (req1 != null) { req1.Abort(); } } preFetchedImg.Clear(); imgReqs.Clear(); //prefetch one by one int cacheCount = CachedImgCount < imgs.Count ? CachedImgCount : imgs.Count; for (int i = 0; i < cacheCount; i++) { WebResponse res = sweb.GetWebResponse(imgs[i].PreviewUrl, proxy, 9000, shc); System.IO.Stream str = res.GetResponseStream(); if (!preFetchedImg.ContainsKey(imgs[i].PreviewUrl)) { preFetchedImg.Add(imgs[i].PreviewUrl, MainWindow.MainW.CreateImageSrc(str)); } } } catch { //Console.WriteLine("useless"); } }))).Start(); }
/// <summary> /// 下载,另一线程 /// </summary> /// <param name="o"></param> private void Download(object o) { DownloadTask task = (DownloadTask)o; FileStream fs = null; Stream str = null; SessionClient sc = new SessionClient(); System.Net.WebResponse res = null; double downed = 0; DownloadItem item = downloadItemsDic[task.Url]; try { res = sc.GetWebResponse( task.Url, MainWindow.WebProxy, task.NeedReferer ); /////////开始写入文件 str = res.GetResponseStream(); byte[] bytes = new byte[5120]; fs = new FileStream(task.SaveLocation + DLEXT, FileMode.Create); int bytesReceived = 0; DateTime last = DateTime.Now; int osize = str.Read(bytes, 0, bytes.Length); downed = osize; while (!task.IsStop && osize > 0) { fs.Write(bytes, 0, osize); bytesReceived += osize; DateTime now = DateTime.Now; double speed = -1; if ((now - last).TotalSeconds > 0.6) { speed = downed / (now - last).TotalSeconds / 1024.0; downed = 0; last = now; } Dispatcher.Invoke(new DownloadHandler(web_DownloadProgressChanged), res.ContentLength, bytesReceived / (double)res.ContentLength * 100.0, task.Url, speed); osize = str.Read(bytes, 0, bytes.Length); downed += osize; } } catch (Exception ex) { //Dispatcher.Invoke(new UIdelegate(delegate(object sender) { StopLoadImg(re.Key, re.Value); }), ""); task.IsStop = true; Dispatcher.Invoke(new VoidDel(delegate() { //下载失败 if (downloadItemsDic.ContainsKey(task.Url)) { item.StatusE = DLStatus.Failed; item.Size = "下载失败"; WriteErrText(task.Url); WriteErrText(task.SaveLocation); WriteErrText(ex.Message + "\r\n"); try { if (fs != null) { fs.Close(); } if (str != null) { str.Close(); } if (res != null) { res.Close(); } File.Delete(task.SaveLocation + DLEXT); DelDLItemNullDirector(item); } catch { } } })); } finally { if (fs != null) { fs.Close(); } if (str != null) { str.Close(); } if (res != null) { res.Close(); } } if (task.IsStop) { //任务被取消 Dispatcher.Invoke(new VoidDel(delegate() { if (downloadItemsDic.ContainsKey(task.Url)) { if (!dlerrtxt.Contains(item.Size)) { item.StatusE = DLStatus.Cancel; } } })); try { File.Delete(task.SaveLocation + DLEXT); DelDLItemNullDirector(item); } catch { } } else { //下载成功完成 Dispatcher.Invoke(new VoidDel(delegate() { try { //DownloadTask task1 = obj as DownloadTask; //判断完整性 if (!item.NoVerify && 100 - item.Progress > 0.001) { task.IsStop = true; item.StatusE = DLStatus.Failed; item.Size = "下载未完成"; try { File.Delete(task.SaveLocation + DLEXT); DelDLItemNullDirector(item); } catch { } } else { //修改后缀名 File.Move(task.SaveLocation + DLEXT, task.SaveLocation); item.Progress = 100.0; item.StatusE = DLStatus.Success; //downloadItemsDic[task.Url].Size = (downed > 1048576 //? (downed / 1048576.0).ToString("0.00MB") //: (downed / 1024.0).ToString("0.00KB")); numSaved++; } } catch { } })); } //下载结束 Dispatcher.Invoke(new VoidDel(delegate() { webs.Remove(task.Url); RefreshList(); })); }