Thread  myThread = new Thread(new ThreadStart(RunMonitor));
myThread.Start(); 
protected void RunMonitor()
        {
            WebBrowser myBrowser = new WebBrowser();
            double lastPrice = 0;
            double curPrice = 0;
            while (true)
            {
                myBrowser.Navigate("http://www.google.com")  //发生异常
                Thread.Sleep(1000);  
            }
         }异常为TargetInvocationException:Unable to get the window handle for the 'WebBrowser' control. Windowless ActiveX controls are not supported.上面得myBrowser并不是UI线程得,所以应该没问题,但是异常,怎么回事呢,和ActiveX有什么关系啊,没接触过,高手请指点。
我用得是2005。

解决方案 »

  1.   

    windows应用中,webbrowser总要先直接或者间接增加到一个窗体中吧。先解决这个问题。另外,多线程增加webbrowser也一样会涉及到Invoke的问题。因为你要把webbrowser增加到其它线程创建的窗体或控件中。建议:webbrowser不要用多线程创建,就算你要有多线程,也应固定个数,然后在主线程提前创建固定个数的webbrowser,每一个线程使用一个,各自通过invoke访问需要的网址。
      

  2.   

    试试Thread  myThread = new Thread(new ThreadStart(ThreadCreat));
    myThread.Start(); 
    private void ThreadCreat()   //通过委托处理
            {
                MethodInvoker In = new MethodInvoker(this.Creat);
                this.BeginInvoke(In);
            }
            private void Creat()
            {
    执行
      

  3.   

    谢谢楼上,异常是没有了,
    但是webrowser不能完成对页面得访问是怎么回事呢,写在while(true)里面似乎永远不能将页面load下来,因为没等load下来又进行另一次load,怎么解决这个问题呢
      

  4.   

    to 但是webrowser不能完成对页面得访问是怎么回事呢,写在while(true)里面似乎永远不能将页面load下来,因为没等load下来又进行另一次load,怎么解决这个问题呢你可以如下进行处理
    private void ThreadCreat() //通过委托处理
    {
          MethodInvoker In = new MethodInvoker(this.Open);
          while (true)
         {
               this.Invoke(In);
               Thread.Sleep(1000);
         } 
    }private WebBrowser myBrowser = new WebBrowser();
    private Mutex mUniqueOpen = new Mutex();
    private void DownloadCompleted( object sender, WebBrowserDocumentCompletedEventArgs e )
    {
          mUniqueOpen.ReleaseMutex();
    }private void Open()
    {
          mUniqueOpen.WaitOne(); 
          myBrowser.Navigate("http://www.google.com")
    }//in form load
    myBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler( DownloadCompleted );
      

  5.   

    谢谢Knight94(愚翁) ,
    不过我试过了,似乎在线程中myBrowser就失去作用了,永远load不下来,换句话说DocumentCompleted 事件永远进不去
    另外我得myBrowser不是在Form中得,而是别得类里面的,没有加到窗体上
      

  6.   

    之所以这样,我其实是为了用我得类来抓网页,但是用HttpWebRequest总是报错,估计是公司得防火墙把我封住了,所以想到用WebBrowser来实现,但是也碰到了问题,真是困难重重啊
      

  7.   

    要是把while(true)去掉就可以进去了,但是程序会死住,这是怎么回事啊
    请高手指点明白,谢谢啦
      

  8.   

    HttpWebRequest报错?一定是哪里没写对。
      

  9.   

    你们是不是上网需要proxy或者用户名/密码的?
      

  10.   

    我试了一下,程序没有什么问题,而是访问google网站,会进行javascript调转,你改成www.baidu.com就行了
      

  11.   

    HttpWebRequest报错?一定是哪里没写对。
    你们是不是上网需要proxy或者用户名/密码的?
      

  12.   

    多线程用WebBrowser...我看算了。还是用HttpWebRequest吧,参考一下这段代码:public string GetUrlHtml(string url)
    {
    string urlWithProtocol = "http://" + url;// *** Establish the request 
    HttpWebRequest http = 
         (HttpWebRequest) WebRequest.Create(urlWithProtocol);// *** Set properties
    http.Timeout = 10000;     // 10 secs
    http.UserAgent = "Web Client";//随便,不过有的网站识别机器人,你就要把IE或其它浏览器的标识抄过来// *** Retrieve request info headers
    HttpWebResponse webResponse = (HttpWebResponse) http.GetResponse();Encoding enc = Encoding.GetEncoding("utf-8");  // 根据实际情况自行调整
    StreamReader responseStream = 
       new StreamReader(webResponse.GetResponseStream(),enc);string html = responseStream.ReadToEnd();webResponse.Close();
    responseStream.Close();return html;
    }
      

  13.   

    Sample code as follows:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace CSOCTDemo
    {
        using System.Threading;
        using System.Diagnostics;
        public partial class frmDemo : Form
        {
            public frmDemo()
            {
                InitializeComponent();
            }        private void frmDemo_Load(object sender, EventArgs e)
            {
                myBrowser.Visible = false;
                this.Controls.Add(myBrowser);
                myBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(myBrowser_DocumentCompleted);
            }        void myBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                 //throw new Exception("The method or operation is not implemented.");
                Debug.WriteLine("Open completed");
                mUniqueOpen.ReleaseMutex();
            }        private void btnGet_Click(object sender, EventArgs e)
            {
                Thread thdSub = new Thread(new ThreadStart(ThreadFun));
                thdSub.Start();
            }        private Mutex mUniqueOpen = new Mutex();
            private WebBrowser myBrowser = new WebBrowser();
            private bool blnStop = false;
            private void ThreadFun()
            {
                MethodInvoker mi = new MethodInvoker(Open);
                while (!blnStop)
                {
                    myBrowser.Invoke(mi);
                    Thread.Sleep(1000);
                }
            }        private void Open()
            {
                mUniqueOpen.WaitOne();
                Debug.WriteLine("Open");
                myBrowser.Navigate("http://www.baidu.com");
            }
        }
    }
      

  14.   

    to aafshzj(上海北京) ( ) 信誉:100    你的方法我早就试过,但是有异常,我在家可以使用,可是公司里不行,一定是公司网络设置得关系,你还有什么意见呢?洗耳恭听
      

  15.   

    sorry渔翁,是我断点的原因,一点都不慢,呵呵星星和裤衩的差距就是大啊,佩服佩服
      

  16.   

    另外,如果你的IE没有任何设置,HttpWebRequest的问题就和你的网络设置无关。我心里知道有一个可能,但你要给我出错信息,我才能判断。
      

  17.   

    你的代码我试过了楼上的,
    WebException:
    "The operation has timed out"StackTrace:
    "   at System.Net.HttpWebRequest.GetResponse()\r\n   at StockReport.Form1.GetUrlHtml(String url) in C:\\Documents and Settings\\waxiangz\\My Documents\\Visual Studio 2005\\Projects\\Test\\Test\\Form1.cs:line 70\r\n   at StockReport.Form1.button5_Click(Object sender, EventArgs e) in C:\\Documents and Settings\\waxiangz\\My Documents\\Visual Studio 2005\\Projects\\Test\\Test\\Form1.cs:line 54\r\n   at System.Windows.Forms.Control.OnClick(EventArgs e)\r\n   at System.Windows.Forms.Button.OnClick(EventArgs e)\r\n   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)\r\n   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)\r\n   at System.Windows.Forms.Control.WndProc(Message& m)\r\n   at System.Windows.Forms.ButtonBase.WndProc(Message& m)\r\n   at System.Windows.Forms.Button.WndProc(Message& m)\r\n   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)\r\n   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)\r\n   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)\r\n   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)\r\n   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)\r\n   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)\r\n   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)\r\n   at System.Windows.Forms.Application.Run(Form mainForm)\r\n   at StockReport.Program.Main() in C:\\Documents and Settings\\waxiangz\\My Documents\\Visual Studio 2005\\Projects\\Test\\Test\\Program.cs:line 17\r\n   at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)\r\n   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)\r\n   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()\r\n   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)\r\n   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n   at System.Threading.ThreadHelper.ThreadStart()"
      

  18.   

    问题全部解决,多谢渔翁,再次佩服,呵呵感谢aafshzj(上海北京),如果HttpWebRequest有什么高见的话,我们再讨论。先结贴啦
      

  19.   

    你可以把
    http.UserAgent = "Web Client";//随便,不过有的网站识别机器人,你就要把IE或其它浏览器的标识抄过来这句改成
    http.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)";试一下。再不行,我也懒得管了。
      

  20.   

    WebBrowser是ActiveX控件,ActiveX控件一定是需要容器的。