private void buttonExport_Click(object sender, EventArgs e) { buttonExport.Enabled = false; LabelActiveThread.Text = "Exporting! All thread are paused"; Thread_Lock = true; ScrapingWorker worker = null; buttonPause.Text = "Resume"; try { worker = new ScrapingWorker(textBoxOutputFileName.Text, "http://www.zoopla.co.uk/for-sale/property/bl9/?include_retirement_homes=true&page_size=24&q=BL9&new_homes=include&include_shared_ownership=true&search_source=for-sale&radius=0&view_type=grid&pn=2"); } catch (Exception w) { } LabelActiveThread.Text = "List Exported at " + DateTime.Now.ToString("h:mm:ss tt"); try { worker.WriteTheList(); } catch (Exception e1) { MessageBox.Show(e1.Message + "\n\tHow to Solve \n1)Check Your Task Manager if csv File is opened \n2)Check if it is used by another Program"); } // Thread_Lock = false; *----> In case of auto resuming the Threads. }
void Form1_DoWork(object sender, DoWorkEventArgs e, int i) { ScrapingWorker worker; string Link; while (!String.IsNullOrEmpty(Link = ListsEncapsulation.IsAnyLeft())) { lock (Link) { textBoxList.Text += "Thread # " + (i + 1) + " is Opening \n"; textBoxList.SelectionStart = textBoxList.Text.Length; textBoxList.Text += Link + "\n"; textBoxList.SelectionLength = Link.Length; textBoxList.SelectionColor = Color.Blue; } while (Thread_Lock) { Thread.SpinWait(1000); } ChangeSubItem(i, 2, Link); ChangeSubItem(i, 1, "30%"); while (Thread_Lock) { Thread.SpinWait(1000); } //Critical Section-- worker can be replaced by another thread try { lock (worker = new ScrapingWorker(textBoxOutputFileName.Text, Link)) { worker.Maine(); } } catch (Exception w) { MessageBox.Show("CanNot Resolve URL, Maybe Server is down or Connection Time out\n\bMore Details\b\n " + w.Message); } while (Thread_Lock) { Thread.SpinWait(100); } ChangeSubItem(i, 1, "70%"); } }