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