1

如何在 asp.net 页面后面的代码中使用 webbrowser 控件。我收到此错误:

无法实例化 ActiveX 控件,因为当前线程不在单线程单元中。

谢谢您的帮助

Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Web.UI.WebControls

Partial Class _Default

    Dim testcontrol As New WebBrowser() ' it breaks here

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        testcontrol.Navigate("mysite")

    End Sub

End Class
4

2 回答 2

1

为什么要在 ASP.NET 应用程序的幕后使用 WebBrowser?如果您需要与另一台服务器上的网页交互,通常人们会使用HttpWebRequest.

如果我错了,有人可以纠正我,但我相信网络应用程序,几乎按照定义,不能是单线程的。Web 应用程序是多用户的,可以做多件事并同时容纳多个用户。

于 2013-02-15T14:28:52.187 回答
0

这是有效的:

/// <summary>
        /// Returns a thumbnail for the current member values
        /// </summary>
        /// <returns>Thumbnail bitmap</returns>
        protected Bitmap GetThumbnail()
        {
            try
            {
            // WebBrowser is an ActiveX control that must be run in a single-threaded
            // apartment so create a thread to create the control and generate the
            // thumbnail
            Thread thread = new Thread(new ThreadStart(GetThumbnailWorker));
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();
            return _bmp;
            }
            catch (Exception ex)
            {
                using (StreamWriter writer = new StreamWriter("log.txt", true))
                {
                    writer.WriteLine(string.Format("[{0}] {1}", DateTime.Now.ToString(), ex.ToString()));
                    writer.Flush();
                    writer.Close();
                }
                return null;
            }
        }


/// <summary>
        /// Creates a WebBrowser control to generate the thumbnail image
        /// Must be called from a single-threaded apartment
        /// </summary>
        protected void GetThumbnailWorker()
        {
            try
            {
                using (WebBrowser browser = new WebBrowser())
                {
                    browser.ClientSize = new Size(_width, _height);
                    //browser.ScrollBarsEnabled = false;
                    browser.ScriptErrorsSuppressed = true;
                    browser.Navigate(_url);
                    // Wait for control to load page
                    while (browser.ReadyState != WebBrowserReadyState.Complete)
                        Application.DoEvents();
                    // Render browser content to bitmap
                    _bmp = new Bitmap(_thumbWidth, _thumbHeight);
                    browser.DrawToBitmap(_bmp, new Rectangle(0, 0, _thumbWidth, _thumbHeight));
                }
            }
            catch (Exception ex)
            {
                using (StreamWriter writer = new StreamWriter("log.txt", true))
                {
                    writer.WriteLine(string.Format("[{0}] {1}", DateTime.Now.ToString(), ex.ToString()));
                    writer.Flush();
                    writer.Close();
                }
            }
        }
于 2013-03-01T08:31:06.870 回答