-2
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//list of tasks
}

private void webBrowser2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//list of tasks
}

see above. my question is, how to make both of them to run at the same time(make two threads to manually control the monitoring of them), now the problem is they all runs separately on the main thread, and i can'd do other tasks, thanks for the helps! sorry about the bad english

and i have tried to use, for example:

    Thread threadw1 = new Thread(() => webBrowser1.Navigate("url1"));
    Thread threadw2 = new Thread(() => webBrowser2.Navigate("url2"));
    threadw1.Start();
    threadw2.Start(); 

but that is to use threads to navigate, once the navigation is done, then the system runs the document completed separately. and i dont know how to control them, thanks for the help!

i have wrapped the tasks in a function and now the problem is when i do this, it doesn't work

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (webBrowser1.ReadyState < WebBrowserReadyState.Complete) return;

                // Do the work, you have the event args in ev


            Thread wbb1 = new Thread(() => wb1task());
            wbb1.Start();
            //wb1task();
        }

but when i do this it works, but it becomes a single thread again:

 private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (webBrowser1.ReadyState < WebBrowserReadyState.Complete) return;

                // Do the work, you have the event args in ev


            //Thread wbb1 = new Thread(() => wb1task());
           // wbb1.Start();
            wb1task();
        }

the thread started then the document completed, i need something to hold after wbb1.Start(); to prevent it to go out of the loop:)

ok this solves the problem, thanks everyone!

wbb1.SetApartmentState(ApartmentState.STA);
  • i have tried to create a thread to handle it, but the function is called automatically after the page loaded completely, and i dont know how to trigger or monitor them manually. – user3847775 Jul 23 '14 at 06:33
  • I suggest you create your threads in the DocumentCompleted functions and have those threads call the function/s to perform the list of tasks (if they are not GUI tasks) – o_weisman Jul 23 '14 at 07:28
  • This is only possible if you create the WebBrowser object on another thread. A special thread, it must meet the STA contract. Covered by: – Hans Passant Jul 23 '14 at 07:48
  • wrap them inside a function then create a thread to run it is probably the valid way, i think i will try it out – user3847775 Jul 23 '14 at 08:13

1 Answers1

0

Put your tasks to run inside the handlers for DocumentCompleted event. I used a thread because you mentioned it. I suggest using either ThreadPool or Task.

private void webBrowser1_DocumentCompleted(object sender, 
        WebBrowserDocumentCompletedEventArgs e)
{
    new Thread((state) => {
        WebBrowserDocumentCompletedEventArgs ev = 
                (WebBrowserDocumentCompletedEventArgs) state;
        // Do the work, you have the event args in ev

    }) {Name = "Thread for webBrowser1"}.Start(e);
}
Alireza
  • 4,976
  • 1
  • 23
  • 36
  • If the page navigates away while the code in your thread is running, that will happen. Are you sure you are staying on the same page? – Alireza Jul 23 '14 at 08:20
  • I don't understand: what do you want to do with a page that is being immediately changed after DocumentCompleted. – Alireza Jul 23 '14 at 08:26
  • oh no, in fact, it is still on the same page, now i am confused again – user3847775 Jul 23 '14 at 08:30
  • i have edited it above, i have only used a thread to wrap them, so strange, i think is the thread is trying to run before the page loaded – user3847775 Jul 23 '14 at 08:36
  • "When the DocumentCompleted event occurs, the new document is fully loaded, which means you can access its contents through the Document, DocumentText, or DocumentStream property" MSDN says – Alireza Jul 23 '14 at 09:14
  • basically navigating through pages and fill the form then submit, 2 browsers operates at the same time, i think i need to mark them as STA , but i dont know how to do that – user3847775 Jul 23 '14 at 11:42
  • I don't think STA to be your problem, but anyway apply `[STAThread]` attribute to the entry point of your application (usually `Main()`) – Alireza Jul 23 '14 at 11:45
  • the problem is message pump stuck, i need to pass the webbrowser object into a new thread as Hans Passant mentioned above – user3847775 Jul 23 '14 at 11:52
  • 1
    wbb1.SetApartmentState(ApartmentState.STA) this solves the problem – user3847775 Jul 23 '14 at 12:24