Imri Barr

Imri Barr

  • NA
  • 42
  • 69.5k

Threading Problem

Oct 7 2011 9:41 PM
Hey,

I'm trying to do something rather something that somehow got a bit complicated on the way.

I wanted to have a loading image in a WPF application while doing some httpwebrequests and finally when all is done, just hide the loading image.

I solved the first problem by using threads. I showed the loading image, then called a thread which handled the httpwebrequests and then after all was done I hid the loading image.

The next problem was to try and get WebBrowser to work. I got this error when trying to use it: ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.

on this line: System.Windows.Forms.WebBrowser webBrowser1 = new System.Windows.Forms.WebBrowser();

Then, I tried declaring the WebBrowser outside of the threaded function and use the invoke function to use it:
webBrowser.Invoke(new Action(delegate {
                //webBrowser.Navigate(lastUrl + url);
                webBrowser.Navigate("http://google.com");
                content = webBrowser.DocumentText;
            }));

All worked smoothly but the only problem is that the webBrowser's properties were all null (could not find google although internet is fine and with httpwebrequest it works fine as well).

Here's the code:
        private delegate void GetSubtitlesParameters(string serieName, int season, int episode);

        private void GetSubtitles(string serieName, int season, int episode)
        {
            loading_image.Visibility = Visibility.Visible;
           
            GetSubtitlesParameters getSubtitlesParameters = new GetSubtitlesParameters(GetSubtitlesThread);
            getSubtitlesParameters.BeginInvoke(serieName, season, episode, null, null);
        }
       
        private void GetSubtitlesThread(string serieName, int season, int episode)
        {
            System.Windows.Forms.WebBrowser webBrowser1 = new System.Windows.Forms.WebBrowser();
            webBrowser1.Navigate("http://google.com");
        }

What am I doing wrong?

Answers (4)