From c771b94f9c8af5125110259948040771cd02c360 Mon Sep 17 00:00:00 2001 From: Aidan Fray Date: Mon, 4 Dec 2017 12:41:01 +0000 Subject: [PATCH] Changes; -Threading has now been added to the dump process, the UI thread should now be free and the UI should remain responsive --- ImageScraper/Form1.cs | 92 ++++++++++++++++++++++++++++--------------- 1 file changed, 61 insertions(+), 31 deletions(-) diff --git a/ImageScraper/Form1.cs b/ImageScraper/Form1.cs index 891b981..68f62cc 100644 --- a/ImageScraper/Form1.cs +++ b/ImageScraper/Form1.cs @@ -5,6 +5,7 @@ using System.Windows.Forms; using System.Diagnostics; using System.IO.Compression; +using System.Threading; namespace _ImageScraper { @@ -39,12 +40,11 @@ private void Form1_Load(object sender, EventArgs e) /// void DumpNLogEverything(List> imageLists) { - if(check_duplicates.Checked == false) + if (check_duplicates.Checked == false) { imageLists = ImageScrape.RemoveAllDuplicates(imageLists); } - progessBar_dump.Maximum = 0; foreach (var item in imageLists) { @@ -60,7 +60,6 @@ void DumpNLogEverything(List> imageLists) textBox_log.Update(); Bitmap tmp = ImageScrape.GetImageFromURL(listItem); - //## Added code - Aidan Fray ## //Grabs extension string[] parts = listItem.Split('.'); string extension = parts[parts.Length - 1]; //Assumes the last section is the file name @@ -75,6 +74,7 @@ void DumpNLogEverything(List> imageLists) } } } + /// /// Just returns all entries in the imageList (the imageList concists of multiple lists) This will count trough them /// @@ -101,50 +101,80 @@ int GetMaxCount(List> list) private void Dump_Click(object sender, EventArgs e) { ImageScrape.LoadFilter(); - if(ImageScrape.FilterList.Count <= 0) + if (ImageScrape.FilterList.Count <= 0) { MessageBox.Show("You need to add an filter first! (ex: .png, .bmp, .gif)"); } else { Duration1.Text = "..."; - timer.Restart(); progessBar_dump.Value = 0; - string webUrl = ImageScrape.PrepareUrl(textBox_url.Text); textBox_log.Text = ""; + timer.Restart(); - webBrowser.Navigate(webUrl); + //Sets of the dumping in another thread + Dump_Thread(); } } - private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) + /// + /// The method that deals with threading the dump functionality + /// + private void Dump_Thread() { - System.IO.File.WriteAllText("dumpedCode.txt", webBrowser.DocumentText); - System.IO.Directory.CreateDirectory("dumpedImages"); - - var images = webBrowser.Document - .GetElementsByTagName("img") - .OfType() - .Select(x => x.GetAttribute("src")) - .ToList(); - - List> dumpingList = ImageScrape.GetAllImageLinks(images); - - label_progress.Text = "0/" + GetMaxCount(dumpingList); - - DumpNLogEverything(dumpingList); + try + { + Thread t = new Thread(() => + { + string webUrl = ImageScrape.PrepareUrl(textBox_url.Text); + webBrowser.Navigate(webUrl); + Application.Run(); + }); + t.SetApartmentState(ApartmentState.STA); + t.Start(); + } + catch (Exception e) + { + MessageBox.Show(e.Message); + } + } - if (check_openDirectory.Checked == true) - Process.Start("dumpedImages"); + private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) + { + try + { + System.IO.File.WriteAllText("dumpedCode.txt", webBrowser.DocumentText); + System.IO.Directory.CreateDirectory("dumpedImages"); - pictureBox_preview.Image = ImageScrape.DumpedList[0].Image; + var images = webBrowser.Document + .GetElementsByTagName("img") + .OfType() + .Select(x => x.GetAttribute("src")) + .ToList(); - maxShow = ImageScrape.DumpedList.Count; - timer.Stop(); - Duration1.Text = timer.ElapsedMilliseconds + " ms"; - pictureBox_preview.Image = ImageScrape.DumpedList[0].Image; + List> dumpingList = ImageScrape.GetAllImageLinks(images); - maxShow = ImageScrape.DumpedList.Count; + //Threading - there a cleaner way to do this? + //All this code needs to be run for the thread it was created on or there will be problems + Invoke(new Action(() => + { + //Main functionality + DumpNLogEverything(dumpingList); + pictureBox_preview.Image = ImageScrape.DumpedList[0].Image; + maxShow = ImageScrape.DumpedList.Count; + + //Shows how long the operation took + timer.Stop(); + Duration1.Text = timer.ElapsedMilliseconds + " ms"; + })); + + if (check_openDirectory.Checked == true) + Process.Start("dumpedImages"); + } + catch (Exception ex) + { + MessageBox.Show("WebBrowser: " + ex.Message); + } } private void ClearDump_Click(object sender, EventArgs e) @@ -239,7 +269,7 @@ private void dataGridView1_CellContentClick(object sender, DataGridViewCellEvent private void check_duplicates_CheckedChanged(object sender, EventArgs e) { - if(check_duplicates.Checked == true) + if (check_duplicates.Checked == true) { check_duplicates.Text = "dump duplicates"; }