Threads are complicated. Even experts in them avoid using them directly when they can. Please use Task
s whenever possible. This is the official recommendation and there are reasons for this. Avoid using old examples and even using new ones, expect lots of problems with complex technology (several things in your code use things not recommended). Without the complete mastery of what is being used it is difficult to develop something advanced.
In this question in the OS there is an implementation of an asynchronous WebBrowser
which I think is what you really want .
The implementation is not the best but it is a start. The answer from Eric Lippert gives indications of what can be improved and the problems that exist in this implementation.
public class WebBrowserAsync {
protected WebBrowser WebBrowser;
private ManualResetEventmre = new ManualResetEvent(false);
public void SetBrowser(WebBrowser browser) {
this.WebBrowser = browser;
browser.LoadCompleted += new LoadCompletedEventHandler(WebBrowser_LoadCompleted);
}
public async void NavigateAsync(string url, Action action) {
Navigate(url);
await Task.Factory.StartNew((Action)(() => { //isto ainda me parece certo
mre.WaitOne();
mre.Reset();
}));
action();
}
public void Navigate(string url) {
WebBrowser.Navigate(new Uri(url));
}
void WebBrowser_LoadCompleted(object sender, NavigationEventArgs e) {
mre.Set();
}
}
Usage:
public async void NavigateToGoogle() {
var browser = new WebBrowser();
await browser.NavigateAsync("www.google.com", () => CopyableMessageBox.Show("WEB URL: " + wm_addr + " LOADED!"));
}
With this class replacing WebBrowser
it is already easier to get the desired result. I am responding more not to leave without some response. This implementation is naive, does not include everything that needs to be done. Getting you ready would take hours.
One of the things missing from it is a way to do something when the document load is completed. In fact to leave this asynchronous version running completely separate at the same level as the official .Net version it would take days to implement.
The version written in VB.Net is a bit better but would have to translate. Look especially at WhenDocumentCompleted
.
Read more about the subject .
I do not know if it helps you but that's what I can answer. Other than that I would have to spend hours or days studying the subject for something I am not needing.
If you want to continue doing with Thread
because it seems to be easier, good luck at understanding all the implications of doing this correctly using GUI. It's an interesting journey.
An addition to breakpoint : Do not use software that is poorly installed. Fix this problem. Although I doubt that a bad installation will let you create applications and do not let using breakpoint . Learning debugar is one of the most important things a developer should know. And debug parallel tasks is something extremely complex. Knowing how to follow the flow of execution already helps to understand what is happening.