WebBrowser Control in a new thread

前端 未结 4 1446
执笔经年
执笔经年 2020-11-21 06:24

I have a list Uri\'s that I want \"clicked\" To achieve this I\"m trying to create a new web-browser control per Uri. I create a new thread per Uri. The problem I\'m having

4条回答
  •  忘了有多久
    2020-11-21 06:51

    A simple solution at which the simultaneous operation of several WebBrowsers occurs

    1. Create a new Windows Forms application
    2. Place the button named button1
    3. Place the text box named textBox1
    4. Set properties of text field: Multiline true and ScrollBars Both
    5. Write the following button1 click handler:

      textBox1.Clear();
      textBox1.AppendText(DateTime.Now.ToString() + Environment.NewLine);
      int completed_count = 0;
      int count = 10;
      for (int i = 0; i < count; i++)
      {
          int tmp = i;
          this.BeginInvoke(new Action(() =>
          {
              var wb = new WebBrowser();
              wb.ScriptErrorsSuppressed = true;
              wb.DocumentCompleted += (cur_sender, cur_e) =>
              {
                  var cur_wb = cur_sender as WebBrowser;
                  if (cur_wb.Url == cur_e.Url)
                  {
                      textBox1.AppendText("Task " + tmp + ", navigated to " + cur_e.Url + Environment.NewLine);
                      completed_count++;
                  }
              };
              wb.Navigate("https://stackoverflow.com/questions/4269800/webbrowser-control-in-a-new-thread");
          }
          ));
      }
      
      while (completed_count != count)
      {
          Application.DoEvents();
          Thread.Sleep(10);
      }
      textBox1.AppendText("All completed" + Environment.NewLine);
      

提交回复
热议问题