Threads are complicated. Even experts in them avoid using them directly when they can. Prefer to use Task
s where possible. This is the official recommendation and there are reasons for this. Avoid using old examples and even using new examples, expect many problems with complex technology (several things in your code use things not recommended). Without the complete mastery of what you are using it is difficult to develop something advanced.
In that question in SO there is an implementation of a WebBrowser
asynchronous that I think is what you really want.
The implementation is not the best, but it is a start. Eric Lippert’s response gives an indication of what can be improved and the problems in that 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();
}
}
I put in the Github for future reference.
Use:
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 the WebBrowser
It is already easier to get the desired result. I am answering more not to leave without any answer. This implementation is naive, does not contemplate all that is necessary to do. To give you something appropriate ready would take hours.
One of the things it lacks is a way to execute something when the document load is completed. In fact to let this asynchronous version run completely separate at the same level as the official version of . NET would take days to implement.
The version written in VB.NET is a little better but would have to translate. Look especially the WhenDocumentCompleted
.
Read more on the subject.
I don’t know if it helps you, but it’s what I can answer. Other than that I would have to spend hours or days studying the subject for something I’m not needing.
If you want to keep doing it with Thread
because it seems to be easier, good luck in understanding all the implications of doing this correctly using GUI. It’s still an interesting journey.
An addendum on the 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 not let use the breakpoint. Learn thresh is one of the most important things a developer should know. And thresh parallel tasks is something extremely complex. Knowing how to follow the flow of execution already helps to understand what is happening.
Did you put a breakpoint at the beginning of the method? If ever, put a Messagebox at the beginning of the Nav and see if it appears....
– Metalus
no! I just created this thread with the functions!
– FRNathan13
Put a breakpoint then at the beginning of the "Nav" method and go by step and see if it doesn’t generate Exception or something like that
– Metalus
and just remembering this thread is in Single Thread Appartament mode if there would be an Exception stating that Active-X failed to implement control because of a Thread call!
– FRNathan13
@Metalus give me an example!
– FRNathan13
Breakpoint dude, you’re using Visual Studio or some IDE that supports this?
– Metalus
Visual Studio but had a bug in the installation that does not show the breakpoint option!
– FRNathan13
Let’s go continue this discussion in chat.
– Metalus
The answer solved the problem? Or at least helped? You need to change something to accept it?
– Maniero