简体   繁体   English

如何使用许多Web浏览器使用线程

[英]How to use threads using 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 . 我正在开发一个应用程序,并且想要创建多个线程,每个线程必须创建一个WebBrowser ,其中每个WebBrowser都使用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. 每个创建的WebBrowser实例如何具有自己的DocumentCompleted处理程序,而不是在所有实例中都具有相同的webBrowser_DocumentCompleted方法。

I explain : 我解释 :

in one case, an operation with a single web browser 在一种情况下,使用单个Web浏览器进行的操作

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 : 但是,如果我想进行多项操作,即在butoon1中,我要添加:

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. 因此,由线程创建的每个Web浏览器必须具有自己的方法webBrowser_DocumentCompleted ,以免在其他Web浏览器的结果之间造成混淆。

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. 或者,对所有创建的Web浏览器使用相同的方法webBrowser_DocumentCompleted ,但是问题是如何指定哪个Web浏览器,请调用方法webBrowser_DocumentCompleted。

You're not making several webbrowsers in your threads, you're still just using the one created somewhere you haven't told us. 您不是在线程中创建多个Web浏览器,而是仍在使用未告诉我们的地方创建的Web浏览器。

Try actually make the webbrowser objects in the methode function 尝试在methode函数中实际制作webbrowser对象

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的两个值吗?

if(a==1 || a==2)
     methode2(wb); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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