2

I found the following code and am trying to implement it in a COM module:

        public Bitmap GetThumbnail()
        {
            ThreadStart _threadstart = new ThreadStart(GenerateThumbnail);
            Thread _thread = new Thread(_threadstart);

            _thread.SetApartmentState(ApartmentState.STA);
            _thread.Start();
            _thread.Join();

            return _image;
        }

        private void GenerateThumbnail()
        {
            WebBrowser _browser = new WebBrowser();
            _browser.ScrollBarsEnabled = false;
            _browser.ScriptErrorsSuppressed = true;
            _browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(_browser_DocumentCompleted);
            _browser.Navigate(_url);
            while (_browser.ReadyState != WebBrowserReadyState.Complete)
            {
                Application.DoEvents();
            }
            _browser.Dispose();
        }

        void _browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser _browser = (WebBrowser)sender;
            _browser.ClientSize = new Size(this._pagewidth, this._pageheight);
            _browser.ScrollBarsEnabled = false;
            _image = new Bitmap(_browser.Bounds.Width, _browser.Bounds.Height);
            _browser.BringToFront();
            _browser.DrawToBitmap(_image, _browser.Bounds);

            if (_imageheight != 0 && _imagewidth != 0)
                _image = (Bitmap)_image.GetThumbnailImage(_imagewidth, _imageheight, null, IntPtr.Zero);
        }

However, I've discovered that the Application.DoEvents() and COM do not work together. The code works great in a C# application, but it hangs when called via COM. As you can tell, I'm trying to get a website thumbnail, and I can't seem to figure out a substitute for the DoEvents().

Any suggestions?

Thanks, Kevin

winepress
  • 21
  • 1
  • 1
    See http://stackoverflow.com/questions/4269800/c-webbrowser-control-in-a-new-thread/4271581#4271581 I doubt it makes a difference. "Via COM" is too vague. – Hans Passant Sep 28 '11 at 18:41
  • Hi Hans, I'm calling this COM object through classic ASP. I don't mean to be vague, but I'm not sure what information you need in order to help. I'll check out the other post and let you know how it works. – winepress Sep 28 '11 at 20:07
  • Hi Hans, Sorry, but that post doesn't help. The problem is that GenerateThumbnail() must wait until the DocumentCompleted event handler is finished. But the thread won't wait for that event to complete on its own. When I add a loop to test the thread state, the application just locks up. Application.DoEvents() is the only thing that seems to remotely works, yet it even locks up the module. – winepress Sep 28 '11 at 20:47
  • Yes, I didn't think so. Classic ASP has a weird threading model, I don't know anything about it. You'll probably need Microsoft Support to get to the bottom of this. – Hans Passant Sep 28 '11 at 20:51

0 Answers0