sir_thursday
sir_thursday

Reputation: 5419

While loop interrupting webBrowser navigation?

I have a method TestScenarios() which is supposed to loop through a database of scenarios, and for each item in the database, navigate to a specified url, take a screenshot, and change some values.

The line I'm having trouble with is the webBrowser1.Navigate(Url); line. When I step through the function, Url has the value of http://google.com, and yet the web control never displays.

Instead, it just says something along the lines of "Navigation has been canceled," which leads me to believe that something is interrupting the Navigate() function from completing.

Furthermore, if I stick a line webBrowser1.Navigate("http://google.com"); outside of the while loop, it shows the web control, which means that the webBrowser controls seems to be functioning properly.

Basically I just need some help with direction. Code is below.

public void TestScenarios()
{
    using (SqlConnection conn = new SqlConnection(connectionString))
    {
        conn.Open();
        using (SqlCommand cmd = new SqlCommand("SELECT * FROM Scenarios WHERE MarkedForDeletion!='1'", conn))
        {
            SqlDataReader reader = cmd.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    // Store scenario information
                    int Id = (int) reader["ScenarioID"];
                    string Data = reader["ScenarioData"].ToString();
                    string Url = reader["ScenarioURL"].ToString();

                    // Navigate to webBrowser
                    webBrowser1.Navigate(Url);

                    // Do test
                    int HasSucceeded = 0;
                    if (Url == "http://bing.com")
                    {
                        HasSucceeded = 1;
                    }

                    // Take Screenshot
                    Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
                    Graphics g = Graphics.FromImage(bitmap);
                    g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);
                    ImageConverter converter = new ImageConverter();
                    Byte[] screenshot = (byte[])converter.ConvertTo(bitmap, typeof(byte[]));

                    // Insert results
                    InsertResults(Id, HasSucceeded, screenshot);

                    // Delete scenario
                    // DeleteScenario(Id);

                    // Mark scenario for deletion
                    MarkScenario(Id);
                }
            }
            reader.Close();
        }
    }
}

Upvotes: 1

Views: 597

Answers (1)

Dan Drews
Dan Drews

Reputation: 1976

Web Browser.navigate takes place asynchronously. So it is navigating (making the page request) then immediately moving on to the next call if url== "http://bing.com"

You want this to be asynchrounous, so you need to setup an event handler to handle the navigation completed event

This Question has some information for you on how you would apply that

Upvotes: 2

Related Questions