public static CancellationTokenSource AddDownloadJob(Syousetsu.Constants details, ProgressBar pb) { int max = Convert.ToInt32(pb.Maximum); int i = 0; int upTo = -1; if (details.Start != String.Empty && details.End == String.Empty)//determine if user don't want to start at chapter 1 { i = Convert.ToInt32(details.Start); } else if (details.Start == String.Empty && details.End != String.Empty)//determine if user wants to end at a specific chapter { i = 1; upTo = max; } else if (details.Start != String.Empty && details.End != String.Empty) //determine if user only wants to download a specifc range { i = Convert.ToInt32(details.Start); //get start of the range upTo = max; //get the end of the range } else { i = 1;//if both textbox are blank assume user wants to start from the first chapter "http://*.syosetu.com/xxxxxxx/1" until the latest/last one "http://*.syosetu.com/xxxxxxx/*" } CancellationTokenSource ct = new CancellationTokenSource(); Task.Factory.StartNew(() => { for (int ctr = i; ctr <= max; ctr++) { string subLink = details.Link + ctr; string[] chapter = Create.GenerateContents(details, GetPage(subLink, details.SyousetsuCookie), ctr); Create.SaveFile(details, chapter, ctr); pb.Dispatcher.Invoke((Action)(() => { pb.Value = ctr; })); if (upTo != -1 && ctr > upTo)//stop loop if the specifed range is reached { break; } if (ct.IsCancellationRequested) { // another thread decided to cancel break; } } pb.Dispatcher.Invoke((Action)(() => { pb.Value = max; pb.ToolTip = null; })); }, ct.Token); return(ct); }
public static void AddDownloadJob(Syousetsu.Constants details) { int max = Convert.ToInt32(details.End); int i = 0; int upTo = -1; if (details.Start != String.Empty && details.End == String.Empty)//determine if user don't want to start at chapter 1 { i = Convert.ToInt32(details.Start); } else if (details.Start == String.Empty && details.End != String.Empty)//determine if user wants to end at a specific chapter { i = 1; upTo = max; } else if (details.Start != String.Empty && details.End != String.Empty) //determine if user only wants to download a specifc range { i = Convert.ToInt32(details.Start); //get start of the range upTo = max; //get the end of the range } else { i = 1;//if both textbox are blank assume user wants to start from the first chapter "http://*.syosetu.com/xxxxxxx/1" until the latest/last one "http://*.syosetu.com/xxxxxxx/*" } for (int ctr = i; ctr <= max; ctr++) { string subLink = details.Link + ctr; string[] chapter = Create.GenerateContents(details, GetPage(subLink, details.SyousetsuCookie), ctr); Create.SaveFile(details, chapter, ctr); if (upTo != -1 && ctr > upTo)//stop loop if the specifed range is reached { break; } } }
public static CancellationTokenSource AddDownloadJob(Syousetsu.Constants details, ProgressBar pb, Label lb) { int max = Convert.ToInt32(pb.Maximum); int i = 0; int upTo = -1; if (details.Start != String.Empty && details.End == String.Empty)//determine if user don't want to start at chapter 1 { i = Convert.ToInt32(details.Start); } else if (details.Start == String.Empty && details.End != String.Empty)//determine if user wants to end at a specific chapter { i = 1; upTo = max; } else if (details.Start != String.Empty && details.End != String.Empty) //determine if user only wants to download a specifc range { i = Convert.ToInt32(details.Start); //get start of the range upTo = max; //get the end of the range } else { i = 1;//if both textbox are blank assume user wants to start from the first chapter "http://*.syosetu.com/xxxxxxx/1" until the latest/last one "http://*.syosetu.com/xxxxxxx/*" } CancellationTokenSource ct = new CancellationTokenSource(); Task.Factory.StartNew(() => { bool cancelled = false; for (int ctr = i; ctr <= max; ctr++) { string subLink; if (details.Site() == Constants.SiteType.Syousetsu) // syousetsu { subLink = details.Link + ctr; } else // kakuyomu { subLink = details.Link + "/episodes/" + details.GetChapterByIndex(ctr).number; } string[] chapter = Create.GenerateContents(details, GetPage(subLink, details), ctr); Create.SaveFile(details, chapter, ctr); // update downloaded history details.LastDownloaded = ctr; History.SaveNovel(details); pb.Dispatcher.Invoke((Action)(() => { pb.Value = ctr; })); if (upTo != -1 && ctr > upTo)//stop loop if the specifed range is reached { break; } if (ct.IsCancellationRequested) { // another thread decided to cancel cancelled = true; break; } } pb.Dispatcher.Invoke((Action)(() => { //pb.Value = max; pb.ToolTip = null; pb.Tag = 1; if (cancelled) { lb.Content = "download aborted - " + lb.Content; //lb.Background = Brushes.MistyRose; } else { pb.Value = max; lb.Content = "finished - " + lb.Content; //lb.Background = Brushes.Aquamarine; } })); }, ct.Token); return(ct); }