Thread starts but there is no reaction

Asked

Viewed 155 times

1

I’m in a little trouble that when I start one thread with a normal function or the tasks of the thread are fully completed and the thread is finalized!

But I need to start one thread that causes a WebBrowser load the tab without locking the window!

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 would be the WebBrowser load the desired URL already defined in the function! but simply the thread starts but the thread starts but does not execute any of the requested actions!

  • 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....

  • no! I just created this thread with the functions!

  • 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

  • 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!

  • @Metalus give me an example!

  • Breakpoint dude, you’re using Visual Studio or some IDE that supports this?

  • Visual Studio but had a bug in the installation that does not show the breakpoint option!

  • The answer solved the problem? Or at least helped? You need to change something to accept it?

Show 4 more comments

1 answer

1

Threads are complicated. Even experts in them avoid using them directly when they can. Prefer to use Tasks 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.

  • threads are not complicated. You can make your code as a normal routine, and after it works put the code inside a thread.

  • You can have this opinion. Experts in threads They say they’re complicated, they even create better abstractions so they don’t have to deal with them. It is precisely because I think they are simple that there is the greatest danger. People use indiscriminately without understanding all the implications.

  • Again I disagree, because I am an expert in precisely this area.

  • I have no way of knowing. I only know of the people I can certify that they consider complicated and recommend avoiding their use. So your statement indicates the opposite. Either way you can answer the question the way you think best.

  • Each acquires knowledge in an area of interest, my focus is background, programming at the level of operating system, so multi-thread is inherent in this area. So complexity is inversely proportional to ignorance or mastery of technology.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.