private void _comicCollect_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (e.Action == NotifyCollectionChangedAction.Add) { foreach (var item in e.NewItems) { VM_Comic temp = item as VM_Comic; if (temp == null) { continue; } temp.finishEvent += Temp_finishEvent; } } else if (e.Action == NotifyCollectionChangedAction.Remove) { foreach (var item in e.OldItems) { VM_Comic temp = item as VM_Comic; if (temp == null) { continue; } temp.finishEvent -= Temp_finishEvent; } } }
private void Temp_finishEvent(VM_Comic obj) { lock (this) { ComicCollect.Remove(obj); } }
public VM_Comic(VM_Comic src) { _comic = new M_Comic(); _comic.ComicImage = src.ComicImage; _comic.ComicLink = src.ComicLink; _comic.ComicName = src.ComicName; _comic.ComicNumber = src.ComicNumber; _comic.ImageLink = src.ImageLink; //_comic.ThumbnailLink = src.ThumbnailLink; }
public VM_ShowComic(VM_Comic temp) { _cts = new CancellationTokenSource(); base.ComicLink = temp.ComicLink; base.ComicName = temp.ComicName; ProgressColor = Brushes.Green; ProgressVisibility = Visibility.Collapsed; OnLoading(ComicLink, CookieHelper.GetCookie()); }
private void _ShowImage(object sender) { VM_Comic comic = (VM_Comic)sender; if (comic == null) { return; } using (WebClient client = new WebClient()) { client.Proxy = null; client.Headers["Cookie"] = CookieHelper.GetCookie(); byte[] bit = null; try { var tt = client.DownloadDataTaskAsync(comic.ThumbnailLink); tt.Wait(new TimeSpan(0, 0, 10)); if (client.IsBusy == true) { client.CancelAsync(); return; } else { bit = tt.Result; } } catch (Exception e) { return; } using (MemoryStream ms = new System.IO.MemoryStream(bit)) { var bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = ms; bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.EndInit(); bitmapImage.Freeze(); ms.Dispose(); App.Current.Dispatcher.Invoke(() => { comic.ComicImage = bitmapImage; }); } } GC.Collect(); }
public VM_DWComic(ObservableCollection <VM_Comic> clist) { //ComicCollect = clist; ComicCollect = new VM_Comic_Collect(); foreach (var item in clist) { VM_Comic temp = new VM_Comic(item); ComicCollect.Add(temp); } Task.Run(() => { SpinWait.SpinUntil(() => false, 2000); Guild <VM_Comic> .PublishTaskList(DownloadImage, ComicCollect); }); }
private async void OnLoading(string uri, string cookie) { try { ProgressVisibility = Visibility.Visible; VM_Comic tempVM = new VM_Comic { ComicLink = this.ComicLink, ComicName = this.ComicName }; ComicCollect = await ParseHelper.ParseDeepSearch(tempVM, cookie, _cts.Token); ProgressVisibility = Visibility.Collapsed; } catch (Exception) { } }
private void DownloadImage(object sender) { VM_Comic comic = (VM_Comic)sender; if (comic == null) { return; } WebClient client = new WebClient(); client.Proxy = null; string SavePath = null; SavePath = comic.ComicName.Trim(); SavePath = f_CleanInput(SavePath); if (SavePath.Length > 100) { SavePath = SavePath.Substring(0, 100); } Directory.CreateDirectory(SavePath); client.Headers["Cookie"] = CookieHelper.GetCookie(); // var tt = client.DownloadDataTaskAsync(comic.ImageLink); String photolocation = SavePath + "/" + comic.ComicNumber + ".jpg"; int thread = 0; client.DownloadProgressChanged += (s, e) => { int temp = e.ProgressPercentage; if (temp > thread) { thread += 10; App.Current.Dispatcher.Invoke(() => { comic.ProgressPercentage = thread; comic.ProgressStatus = "Downloading"; }); } //if (temp % 10 == 0) //{ // App.Current.Dispatcher.Invoke(() => // { // comic.ProgressPercentage = temp; // comic.ProgressStatus = "Downloading"; // }); //} }; client.DownloadFileCompleted += (s, e) => { if (!e.Cancelled && e.Error == null) { App.Current.Dispatcher.Invoke(() => { comic.ProgressPercentage = 100; comic.ProgressStatus = "Success"; comic.ProgressColor = (SolidColorBrush)(new BrushConverter().ConvertFrom("#FF017C11")); }); } else { App.Current.Dispatcher.Invoke(() => { comic.ProgressPercentage = 0; comic.ProgressStatus = "Fail"; comic.ProgressColor = (SolidColorBrush)(new BrushConverter().ConvertFrom("#FF7A0000")); }); File.Delete(photolocation); } }; try { var tt = client.DownloadFileTaskAsync(new Uri(comic.ImageLink), photolocation); tt.Wait(new TimeSpan(0, 0, 30)); if (client.IsBusy == true) { client.CancelAsync(); return; } App.Current.Dispatcher.Invoke(() => { comic.OnFinished(); }); } catch (Exception e) { return; } GC.Collect(); }