0

I am developing a c# winform app which controls a website using web browser. I need to perform many events on click of a single button, but many events do not take place until I use a message box to give some sort of lag between events. Below is the code

private void button5_Click(object sender, EventArgs e)
{
    try
    {
        HtmlDocument webDoc = this.webBrowser1.Document;
        HtmlElementCollection aTags = webDoc.GetElementsByTagName("a");
        string selectedIssue;
        selectedIssue = AcknowledgeList.SelectedItem.ToString();
        foreach (HtmlElement aElement in aTags)
        {
            if (aElement.InnerText.Contains(selectedIssue))
            {
                aElement.InvokeMember("click");

                break;
            }
        }
        MessageBox.Show("Device Acknowledged");

        this.finalAcknowledge();
    }
    catch (NullReferenceException connectionError)
    {
        MessageBox.Show("Connection Error , try again");
    }
}

private void finalAcknowledge()
{
    try
    {
        HtmlDocument webDoc = this.webBrowser1.Document;

        HtmlElement changeNote = webDoc.GetElementById("@note");
        string comment = textBox1.Text;
        changeNote.SetAttribute("value", comment);

        HtmlElementCollection selectTags
            = webDoc.GetElementsByTagName("select");
        foreach (HtmlElement selectElement in selectTags)
        {
            if (selectElement.GetAttribute("name").Equals("status"))
            {
                selectElement.SetAttribute("value", "6");
            }
        }

        HtmlElement submitButton = webDoc.GetElementById("submit_button");
        submitButton.InvokeMember("click");
        this.button3.PerformClick();
        string selectedIssue;
        selectedIssue = AcknowledgeList.SelectedItem.ToString();
        AcknowledgeList.Items.Remove(AcknowledgeList.SelectedItem);
        AssignToList.Items.Add(selectedIssue);

        MessageBox.Show("Device Acknowledged");

        this.callShowAssigned();
    }
    catch (NullReferenceException connectionError)
    {
        MessageBox.Show("Connection Error , try again");
    }
}

Here I have used two message boxes to give some lag between events. I want to get rid of these message boxes and want some other method which can perfrom all the events and I do not have to interrupt the user with some message box or something that is visible

Prachur
  • 1,100
  • 7
  • 24
  • 42
  • 2
    Sounds like you are doing something wrong here, you should not have to create "lag" but should check for some kind of semaphore or event that you can note and then do the correct thing when it is in the correct state, trying to rely on lag is a bad idea. – jimplode Apr 12 '11 at 12:34
  • Can you tell WHY you are introducing a lag? – Tony The Lion Apr 12 '11 at 12:36
  • @ jimplode i am new to .net and this is my first app , dont know anything in detailed , so help me by giving pointers to some solution – Prachur Apr 12 '11 at 12:36
  • @ Tony , on click of a button in winform it is clicking a link on the web page which navigates to another page , just after this event i want to do some changes on the second page and click a button on the second page . all theses event occur only when i put a message box , if i remove the message box only the first step would take place , i.e. web page is navigated to second page and i cannot perform any more event on the second page – Prachur Apr 12 '11 at 12:39
  • Take a look at @SLaks answer: http://stackoverflow.com/questions/4109113/how-to-handle-threading-for-webbrowser-control – HABJAN Apr 12 '11 at 12:41
  • Out of morbid curiosity, why are you using a webpage in an application? Do you have control over the webpage and any associated databases>? – jimplode Apr 12 '11 at 12:42
  • using webbrowser control i am controlling the web page , this i am doing coz i do not have access to the database. the app is just a desktop version of a web page – Prachur Apr 12 '11 at 12:47
  • @ Habjan that answer is way above my head – Prachur Apr 12 '11 at 12:51

1 Answers1

2

Lag can be done with:

Thread.Sleep(time_to_sleep);

But in this case better to use event: http://msdn.microsoft.com/en-us/library/5d67hf8a.aspx

webBrowser1.DocumentCompleted+=OnPageLoaded;

When you aElement.InvokeMember("click"), you tell to webBrowser1 make some action: post/get to some page. So webbrowser begins job: it makes call to remote server, gets page, renders it. This take time, which can be longer or shorter. This call is made asynchronous, which means, that your code runs forward without waiting for webbrowser to finish. So what can you do? You can subscribe to webbrowser object events, which will hit when browser control finish work.

//Somewhere before InvokeMember
webBrowser1.DocumentCompleted+=OnPageLoaded;


private void OnPageLoaded(object sender,
        WebBrowserDocumentCompletedEventArgs e)
    {
    //Make your final acknowledgement
    //This method will be executed every time, when your page is loaded
    }
VikciaR
  • 3,324
  • 20
  • 32