简体   繁体   中英

How to use different events across many web browsers

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(methode1));
                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.

thanks in advance

Quite easily, change the name of the method.

When you're creating your WebBrowser , you can assign DocumentCompleted to be any method that you want, for example:

firstWebBrowser.DocumentCompleted += firstDocumentCompleted;
secondWebBrowser.DocumentCompleted += secondDocumentCompleted;
thirdWebBrowser.DocumentCompleted += thirdDocumentCompleted;

If you're using the designer, it's just as simple. In the Events tab, simply type a new (unique) method name.

Since you mentioned threading, I do envisage one problem if you don't pre-empt the amount of web browsers (and thus event handlers) you're going to have. But we'd need more specific use case examples if you want help with that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM