public void CancelDownloads() { // choose target files to download var url = "http://www.smbc-comics.com/comics/20061001.gif"; // var url = "http://www.irregularwebcomic.net/55.html"; var parser = new ComicParser (url); var urlGen = parser.GetUrlGenerator () as DateGenerator; urlGen.Days = DateGenerator.EveryDay; var urls = urlGen.Get (0, 20); foreach (var x in urls) { Console.WriteLine (x); } using (var repo = new Repository ()) { // set the gate AutoResetEvent auto = new AutoResetEvent (false); int num = 0; // event handler to count number of async downloads repo.DownloadFileCompleted += delegate(object sender, AsyncCompletedEventArgs e) { num += 1; // here we set the canceller if (num == 5) { repo.CancelDownloads (); auto.Set (); } }; // event handler allows program to progress (in case of cancel failure) repo.MultipleDownloadsCompleted += delegate(object sender, EventArgs e) { auto.Set (); }; // begin the download and block repo.Download (urls); auto.WaitOne (); // check that it worked Assert.AreEqual (5, repo.Files.Count); } }
public void RunTask() { if (!readyToGo) { throw new ArgumentException ("Not ready to go!"); } else { if (TaskStarted != null) { TaskStarted.Invoke (this, new EventArgs ()); } } Active = true; Status = "Analyzing source"; var urlGen = SetupTask (); var urls = urlGen.Get (0, NumberToDownload); using (repo = new Repository ()) { // pass on the events repo.DownloadFileCompleted += OnSingleDownloadCompleted; repo.DownloadProgressChanged += OnDownloadProgressChanged; repo.MultipleDownloadsCompleted += OnMultipleDownloadsCompleted; // set number downloaded to zero NumberDownloaded = 0; // begin (blocks) repo.Download (urls); // repo.Active is set and unset automatically } }
string TestOneDownload(Repository repo, string imgUrl) { // do download repo.DownloadFileCompleted += delegate(object sender, AsyncCompletedEventArgs e) { auto.Set (); }; bool downloadStartedEvent = false; repo.DownloadStarted += delegate(object sender, EventArgs e) { downloadStartedEvent = true; }; repo.Download (imgUrl); Assert.IsTrue(downloadStartedEvent); // check on Files dict Assert.AreEqual (1, repo.Files.Count, "Only one entry in Files"); var fileUrl = Path.Combine (repo.Location, "00001.gif"); Assert.IsTrue (repo.Files.Contains (new KeyValuePair<string,string> (imgUrl, fileUrl)), "Files contains this entry"); Assert.AreEqual (imgUrl, repo.CurrentlyDownloadingUrl); // wait for auto auto.WaitOne (); Assert.IsTrue (File.Exists (fileUrl), "File exists"); Assert.AreEqual (26823, (new FileInfo (fileUrl)).Length, "File is correct size"); return fileUrl; }
public void DownloadMany() { // choose target files to download var url = "http://www.smbc-comics.com/comics/20061001.gif"; // var url = "http://www.irregularwebcomic.net/55.html"; var parser = new ComicParser (url); var urlGen = parser.GetUrlGenerator () as DateGenerator; urlGen.Days = DateGenerator.EveryDay; var urls = urlGen.Get (0, 20); foreach (var x in urls) { Console.WriteLine (x); } var numComics = urls.Where<string> (x => x != String.Empty).Count (); using (var repo = new Repository ()) { // set the gate AutoResetEvent auto = new AutoResetEvent (false); // event handler allows program to progress repo.MultipleDownloadsCompleted += delegate(object sender, EventArgs e) { auto.Set (); }; // begin the download and block repo.Download (urls); // auto.WaitOne (); // check that it worked Assert.AreEqual (numComics, repo.Files.Count); } }