调用webbrowser控件后面的asp.net页面错误代码?

时间:2013-02-15 13:24:10

标签: c# asp.net .net vb.net

如何在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

2 个答案:

答案 0 :(得分:1)

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

如果我错了,有人可以纠正我,但我相信一个Web应用程序,几乎按照定义,不能是单线程的。 Web应用程序旨在成为多用户,可以执行多项操作并同时容纳多个用户。

答案 1 :(得分: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();
                }
            }
        }