0

I have written some code to post messages to a site. It works just fine (for the first instance), the problem is that it keeps looping (running the SendData method over and over) once it is in the webBrowser1_DocumentCompleted method. So I must not be handling the event correctly. After it has run the SendData call one time I want it return to the button1_Start_Click event from which it originally started.

private void button1_Start_Click(object sender, EventArgs e)
    {



        GetData();


    }


    private void GetData()
    {

        webBrowser1.Navigate(inputURLID);
    }



    private void SendData()
    {

        webBrowser1.Document.GetElementById("subject").SetAttribute("value", textBox2_Subject.Text);//To (username)

        webBrowser1.Document.GetElementById("message").SetAttribute("value", richTextBox1.Text);//Subject

        webBrowser1.Document.GetElementById("Submit").InvokeMember("click");//Message
    }



    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        SendData();
    }
eltel2910
  • 335
  • 2
  • 6
  • 16

2 Answers2

0

The problem is that when you do the click on Submit, a new page is loaded and DocumentCompleted is called again for this new page.

You can try something like this:

bool documentCompleted = false;

private void button1_Start_Click(object sender, EventArgs e)
{
    webBrowser1.Navigate(inputURLID);
    WaitForDocumentCompleted();
    SendData();
    WaitForDocumentCompleted();
}

private void WaitForDocumentCompleted()
{
    while (!documentCompleted)
    {
        Thread.Sleep(100);
        Application.DoEvents(); 
    }

    documentCompleted = false;
}

private void SendData()
{
    webBrowser1.Document.GetElementById("subject").SetAttribute("value", textBox2_Subject.Text);//To (username)
    webBrowser1.Document.GetElementById("message").SetAttribute("value", richTextBox1.Text);//Subject
    webBrowser1.Document.GetElementById("Submit").InvokeMember("click");//Message
}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    documentCompleted = true;
}
fcuesta
  • 4,429
  • 1
  • 18
  • 13
  • Wow that worked really good!!! Thank you so much. Next question...How can I put this all into a loop. So when the whole thing cycles I end right back at the button1_Start_Click method where it will programmatically start again? – eltel2910 Jun 06 '13 at 03:33
  • The problem I have found when I try to loop is that the webBrowser control never refreshes to the next user, it just stays on the "message sent" page. I have watched it in the debugger and the correct next new URLId is loaded up, but the page is never updated to the next uRL when the call is sent out – eltel2910 Jun 06 '13 at 04:05
  • Please, **never** solve a problem with DoEvents(). Programmers that are learning the craft, like the OP, don't yet know how [dangerous](http://stackoverflow.com/questions/5181777/use-of-application-doevents/5183623#5183623) it is. – Hans Passant Jun 06 '13 at 11:01
0

I'll have to post, the answer you selected is going to get you in deep trouble. The workaround is simple, you just need a variable that tells you that the next DocumentCompleted event is the one you are interested in. Like this:

private bool WaitingForData;

private void GetData()
{
    webBrowser1.Navigate(inputURLID);
    WaitingForData = true;
}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if (WaitingForData) SendData();
    WaitingForData = false;
}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thank you, Hans it now works completely. Where could I put the looping logic, so it will run on it's own. I know to tie it to the number of names in the list box, but should I use like a while loop? – eltel2910 Jun 06 '13 at 17:04