Thread starts but there is no reaction

1

I have a small problem that when I start a thread with a normal function the thread tasks are fully completed and the thread is over!

But I need to start a thread which causes a WebBrowser to load the page without the window locking!

Ex:

Thread wm_thread;
string wm_addr="";
void Navega(string addr){
  wm_thread = new Thread(new ThreadStart(nav));
  wm_addr=addr;
  wm_thread.Start();
}

void nav(){
  var wm_browser = new WebBrowser();
  wm_browser.Navigate(wm_addr);
  wm_browser.DocumentCompleted +=new delegate{
    CopyableMessageBox.Show("WEB URL: " + wm_addr + " LOADED!");
  };
}

The right one would be WebBrowser load the desired URL already defined in the function! but the thread starts but the thread starts but does not perform any of the requested actions!

    
asked by anonymous 31.01.2015 / 00:32

1 answer

1

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.

    
31.01.2015 / 09:14