0

I'm working on an application and I want to create multiple threads, each thread must create a WebBrowser, every WebBrowser of these uses the method webBrowser_DocumentCompleted.

How can each of the created WebBrowser instances have it's own DocumentCompleted handler instead of the same webBrowser_DocumentCompleted method across all of them.

I explain :

in one case, an operation with a single web browser

int a = 0;
private void button1_Click(object sender, EventArgs e)
{
    methode1();
}

private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if (a == 1) methode2(wb);
    if (a == 2) methode2(wb);
}

public void methode1()
{
    webBrowser.Navigate("http://www.test.com");
    a = 1;
}

public void methode2()
{
    HtmlElement txt1 = webBrowser1.Document.GetElementById("tesxtbox1");
    txt1.SetAttribute("value", "test");
    webBrowser.Document.Forms[0].InvokeMember("submit");
    a = 2;
}

public void methode3()
{
    webBrowser.Navigate("http://www.test3.com");
}

but if I want to make multiple operation, ie in butoon1 I add :

private void button1_Click(object sender, EventArgs e)
{
    for(int i=0; i<5  ;i++)
        methode1();
}

then to do it, I think I must have several webbrowser, so the solution is to create a thread for each operation

private void button1_Click(object sender, EventArgs e)
{
    for(int i=0; i<5  ;i++)
    {
        Thread thread = new Thread(new ThreadStart(method1));
        thread.Start();
    }   
}

So each web browser created by a thread must have its own method webBrowser_DocumentCompleted, to not be confused between the results of other web browser.

or, use the same method webBrowser_DocumentCompleted for all created web browser, but the problem is how to specify which webbrowser, call the method webBrowser_DocumentCompleted.

quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
Ti Amo Laky
  • 755
  • 2
  • 8
  • 21
  • When you create a WebBrowser object, you should be assigning the event handler to the DocumentCompleted handler, which then guarantees that each discrete WebBrowser instance knows what to do with itself when a document load has completed. You don't "direct" the handler to the "right" instance. That's backwards. – David W Feb 26 '13 at 15:00
  • follow Hans Passant answer in http://stackoverflow.com/questions/4269800/webbrowser-control-in-a-new-thread – volody Feb 26 '13 at 15:48

1 Answers1

0

You're not making several webbrowsers in your threads, you're still just using the one created somewhere you haven't told us.

Try actually make the webbrowser objects in the methode function

private void button1_Click(object sender, EventArgs e)
        {
            for(int i=0;i<5;i++)
                methode1();
        }

public void methode1()
        {
            System.Windows.Forms.WebBrowser wBrowser = System.Windows.Forms.WebBrowser();
            wBrowser.DocumentCompleted +=webBrowser_DocumentCompleted;
            wBrowser.Navigate("http://www.test.com");
            a = 1;
        }

Also, you know you can check both values of a in the one if statement right?

if(a==1 || a==2)
     methode2(wb); 
James
  • 4,146
  • 1
  • 20
  • 35