开发者

multithreading webBrowser does not work

I am trying to code a multithreaded WebBrowser application.

  • The WebBrowser elements will just navigate to a given URL,
  • wait until it loads and then
    • click a button or
    • submit the form.
  • This should happen in a loop forever.

I'm using Microsoft Visual Studio 2010 and Windows Forms

Here's my code:

//added windows form 30 webbrowser object 
//and now assigning them to an webbrowser array

wbList[0] = webBrowser1; wbList[1] = webBrowser2; wbList[2] = webBrowser3; 
wbList[3] = webBrowser4; wbList[4] = webBrowser5; wbList[5] = webBrowser6; 
wbList[6] = webBrowser7; wbList[7] = webBrowser8; wbList[8] = webBrowser9;
//etc. until:
wbList[29] = webBrowser30;

for (int i = 0; i < 30; i++)
{
    wbList[i].ScriptErrorsSuppressed = true;
    wbList[i].NewWindow += new CancelEventHandler(wb_NewWindow);
}

//********************************** creating threads here

Thread[] AllThread = new Thread[100];

int irWhichWbb = 0;
for (int nn = irDirectPostCount; nn < irNumber+1; nn++)
{
    AllThread[nn] = new Thread(new 
                          ParameterizedThreadStart(this.MultiThreadWebBrowser));
    AllThread[nn].Start(nn.ToString() + ";" + irWhichWbb.ToString());
    irWhichWbb++;
}

Application.DoEvents();
for (int nn = 0; nn < irNumber+1; nn++)
{ AllThread[nn].Join(); }

//Multi开发者_如何转开发 thread function
void MultiThreadWebBrowser(object parameter)
{
    string srParam = parameter.ToString();

    int i = Convert.ToInt32 (srParam.Substring(0,(srParam.IndexOf(";"))));
    int irWhichWb = Convert.ToInt32(srParam.Substring(srParam.IndexOf(";")+1));

    string hdrs = "Referer: http://www.xxxxxxxxx.com/xxxxxxxxxx.aspx";

    try
    {
        wbList[irWhichWb].Navigate(srVotingList[i, 0], "_self", null, hdrs);
    }
    catch { }

    try { waitTillLoad(irWhichWb); }
    catch { }
    waitTillLoad3();
}

// wait until webbrowser navigate url loaded
private void waitTillLoad(int irWhichLoad)
{
    WebBrowserReadyState loadStatus;
    //wait till beginning of loading next page  
    int waittime = 100000;
    int counter = 0;
    while (true)
    {
        try
        {
            loadStatus = wbList[irWhichLoad].ReadyState;
            Application.DoEvents();

            if ((counter > waittime) || 
                (loadStatus == WebBrowserReadyState.Uninitialized) || 
                (loadStatus == WebBrowserReadyState.Loading) || 
                (loadStatus == WebBrowserReadyState.Interactive))
            {
                    break;
            }
            counter++;
        }
        catch { }
    }

    //wait till the page get loaded.
    counter = 0;
    while (true)
    {
        try
        {
            loadStatus = wbList[irWhichLoad].ReadyState;
            Application.DoEvents();
            if (loadStatus == WebBrowserReadyState.Complete)
            {
                break;
            }
            if (counter > 10000000)
                break;
            counter++;
        }
        catch { }
    }
}

private void waitTillLoad3()
{
    DateTime dtStart = DateTime.Now;
    while (true)
    {
        if ((DateTime.Now - dtStart).TotalMilliseconds > 4000)
            break;

        Application.DoEvents();
    }
}


You don't say what kind of failure you get: "doesn't work" is not a good description.

I would first try with just a single thread. Does that work?

You have empty catch blocks, so you are silently ignoring some error conditions. This may well by hiding a problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜