Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace dino_test
  13. {
  14. public partial class Form1 : Form
  15. {
  16. public Form1()
  17. {
  18. InitializeComponent();
  19. //Omoguci stvaranje elementi (webbrowsera) iz drugih threadova (nije bas super rjesenje ali za ovaj slucaj je oke)
  20. CheckForIllegalCrossThreadCalls = false;
  21.  
  22. if (!WBEmulator.IsBrowserEmulationSet())
  23. {
  24. WBEmulator.SetBrowserEmulationVersion();
  25. }
  26. }
  27.  
  28. private void runBrowserThread()
  29. {
  30. var th = new Thread(() => {
  31. var br = new WebBrowser();
  32. br.DocumentCompleted += browser_DocumentCompleted;
  33. br.ScriptErrorsSuppressed = true;
  34. br.Navigate(new Uri("https://www.quotes.net/authors/Cometan"));
  35. Application.Run();
  36. });
  37. th.SetApartmentState(ApartmentState.STA);
  38. th.Start();
  39. }
  40.  
  41. void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
  42. {
  43. var br = sender as WebBrowser;
  44. MessageBox.Show(br.DocumentText.ToString());
  45. Application.ExitThread();
  46.  
  47. /*
  48. if (br.Url == e.Url)
  49. {
  50. MessageBox.Show(br.DocumentText.ToString());
  51.  
  52.  
  53. Application.ExitThread();
  54. }
  55. */
  56. }
  57.  
  58.  
  59.  
  60.  
  61. private void Button1_Click(object sender, EventArgs e)
  62. {
  63.  
  64. int n = Int32.Parse(textBox1.Text);
  65.  
  66. Thread[] threadovi = new Thread[n];
  67.  
  68. for (int i=0; i<n; i++)
  69. {
  70. threadovi[i] = new Thread(runBrowserThread);
  71. //threadovi[i].SetApartmentState(ApartmentState.STA); //Zguglaj ovo, bez toga se nebre webbrowser u drugom threadu stvarato
  72. threadovi[i].Start();
  73. }
  74.  
  75. //for (int i = 0; i < n; i++) threadovi[i].Join();
  76.  
  77.  
  78.  
  79.  
  80. }
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement