Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm having trouble with my program flow in a while loop I created.

while (reader.Read())
{
    // Store scenario information
    int Id = (int)reader["ScenarioID"];
    string Data = reader["ScenarioData"].ToString();
    string Url = "http://google.com";

    // Initialize result information
    int HasSucceeded = 0;
    var screenshot = new Byte[] { };

    // Navigate to webBrowser
    webBrowser2.Navigate(Url);
    webBrowser2.DocumentCompleted += WebBrowserDocumentCompleted;

    // Do test
    TestScenarios(Url, HasSucceeded);

    // Take screenshot
    TakeScreenshot(screenshot);

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

    // Mark scenario for deletion
    MarkScenario(Id);
}

private void WebBrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs Url)
{
    MessageBox.Show("Operation has completed!");
}

The expected flow of the program should be

  1. Read an item in the table
  2. Initialize some variables/store some values
  3. Navigate the webBrowser control toe the URL
  4. When the webBrowser control is finished, do a test
  5. Take a screenshot
  6. Insert results into new table
  7. Mark the item in the original table for deletion
  8. Loop back to #1 until all items have been covered.

However, what is happening is everything in the while loop is running properly in order except for the webBrowser2.Navigate line, which does not show the Url until the while loop has exited. Immediately after the Url shows, 5 sequential messages "Operation has completed" (for the 5 items in the table) appear. How can I fix my flow?

share|improve this question

3 Answers 3

up vote 1 down vote accepted

Try this solution. Wrap your loop in another thread than UI thread. then make use of AutoResetEvent

new Thread(() =>
{
    AutoResetEvent signal = new AutoResetEvent(false);
    while (reader.Read())
    {
        // Store scenario information
        int Id = (int)reader["ScenarioID"];
        string Data = reader["ScenarioData"].ToString();
        string Url = "http://google.com";

        // Initialize result information
        int HasSucceeded = 0;
        var screenshot = new Byte[] { };

        Action action = () =>
        {
             webBrowser2.Tag = signal;
             // Navigate to webBrowser
             webBrowser2.Navigate(Url);
             webBrowser2.DocumentCompleted -= WebBrowserDocumentCompleted;
             webBrowser2.DocumentCompleted += WebBrowserDocumentCompleted;
        };
        webBrowser2.Invoke(action);

        signal.WaitOne();//Wait till it finishes

        // Do test
        TestScenarios(Url, HasSucceeded);

        // Take screenshot
        TakeScreenshot(screenshot);

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

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

    private void WebBrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs Url)
    {
        MessageBox.Show("Operation has completed!");
        ((AutoResetEvent)((WebBrowser)sender).Tag).Set();
    }

I asked worker thread to wait till the document loads then continue execution. simple.

Hope this helps

share|improve this answer
    
I get "invalid attempt to call read when reader is closed", but thanks for the example, I'm working on it. –  zelliott Jul 23 '13 at 18:04
    
where are you calling connection.close()? don't call it outside this block instead call connection.close() after the while loop –  Sriram Sakthivel Jul 23 '13 at 18:05
    
So, I call reader.Close(); right after the while loop, but I think it's the Start(); line that's causing the exception, because when I remove that line I don't get an exception, but it still doesn't work. –  zelliott Jul 23 '13 at 18:08
    
I changed some ordering around, working now. thanks. –  zelliott Jul 23 '13 at 18:24

The Navigate method is probably queuing an event which will be later handled on the same thread your code is running in (the UI thread). You may have to put your code into a separate background worker thread to allow the UI events to be processed before your loop is finished.

share|improve this answer

I recomend you to ckeck the async and await operation if you are devleloping in .NET 4.5 Frammework. This propably will solve your problem. Async and Await in MSDN

share|improve this answer
1  
Note that async and await were introduced in .NET 4.5 –  Randy James Jul 23 '13 at 17:20
    
Thats rigth! Thanks for the edit. I just edited my question. –  oimitro Jul 23 '13 at 17:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.