2012-07-06 30 views
1

我想製作一個控制檯程序來監視網頁的htmlsourcecode,因爲一些頁面內容是由一些javescript創建的,所以我必須使用webbrowser控件。像:View Generated Source (After AJAX/JavaScript) in C#.net webbrowser控件在多線程中拋出「EXCEPTION code = ACCESS_VIOLATION」

我的代碼如下:

public class WebProcessor 
{ 
    public string GeneratedSource; 
    public string URL ; 

    public DateTime beginTime; 
    public DateTime endTime; 

    public object GetGeneratedHTML(object url) 
    { 
     URL = url.ToString(); 
     try 
     { 
      Thread[] t = new Thread[10]; 

      for (int i = 0; i < 10; i++) 
      { 
       t[i] = new Thread(new ThreadStart(WebBrowserThread)); 
       t[i].SetApartmentState(ApartmentState.STA); 
       t[i].Name = "Thread" + i.ToString(); 
       t[i].Start(); 
       //t[i].Join();   
      } 

     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.ToString()); 
     } 

     return GeneratedSource; 
    } 

    private void WebBrowserThread() 
    { 
     WebBrowser wb = new WebBrowser(); 
     wb.ScriptErrorsSuppressed = true; 

     wb.DocumentCompleted += 
      new WebBrowserDocumentCompletedEventHandler(
       wb_DocumentCompleted); 

     while(true) 
     { 
      beginTime = DateTime.Now; 

      wb.Navigate(URL); 

      while (wb.ReadyState != WebBrowserReadyState.Complete) 
      { 
       Application.DoEvents(); 
      Thread.Sleep(new Random().Next(10,100)); 
      } 
     }    

     //wb.Dispose();     
    } 

    private void wb_DocumentCompleted(object sender, 
     WebBrowserDocumentCompletedEventArgs e) 
    { 
     WebBrowser wb = (WebBrowser)sender; 
     if (wb.ReadyState == WebBrowserReadyState.Complete) 
     { 
      GeneratedSource= wb.Document.Body.InnerHtml; 

      endTime = DateTime.Now; 
      Console.WriteLine("WebBrowser " + (endTime-beginTime).Milliseconds + Thread.CurrentThread.Name + wb.Document.Title);    
     } 
    } 
} 

,當它運行一段時間(20-50倍)後,扔掉它這樣

---------------------------------------------------------------------------- 
EXCEPTION code=ACCESS_VIOLATION 
(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(nul 
l)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(n 
ull)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null) 
(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(nul 
l)(null)BACKTRACE: 33 stack frames: 
#0 0x083dba8db0 at MatchExactGetIDsOfNames in mshtml.dll 
#1 0x0879f9b837 at StrongNameErrorInfo in mscorwks.dll 
#2 0x0879f9b8e3 at StrongNameErrorInfo in mscorwks.dll 
#3 0x0879f9b93a at StrongNameErrorInfo in mscorwks.dll 
#4 0x0879f9b9e0 at StrongNameErrorInfo in mscorwks.dll 
#5 0x0879f9b677 at StrongNameErrorInfo in mscorwks.dll 
#6 0x0879f9b785 at StrongNameErrorInfo in mscorwks.dll 
#7 0x0879f192a8 at InstallCustomModule in mscorwks.dll 
#8 0x0879f19444 at InstallCustomModule in mscorwks.dll 
#9 0x0879f194ab at InstallCustomModule in mscorwks.dll 
#10 0x0879fa6491 at StrongNameErrorInfo in mscorwks.dll 
#11 0x0879f44bcf at DllGetClassObjectInternal in mscorwks.dll 
#12 0x089bbafa at in 
#13 0x087b18cc10 at in System.Windows.Forms.ni.dll 
#14 0x087b91f4c1 at in System.Windows.Forms.ni.dll 
#15 0x08d00669 at in 
#16 0x08792d6e46 at in mscorlib.ni.dll 
#17 0x08792e02cf at in mscorlib.ni.dll 
#18 0x08792d6dc4 at in mscorlib.ni.dll 
#19 0x0879e71b4c at in mscorwks.dll 
#20 0x0879e896ce at in mscorwks.dll 
#21 0x0879e96ea9 at CoUninitializeEE in mscorwks.dll 
#22 0x0879e96edc at CoUninitializeEE in mscorwks.dll 
#23 0x0879e96efa at CoUninitializeEE in mscorwks.dll 
#24 0x0879f88357 at GetPrivateContextsPerfCounters in mscorwks.dll 
#25 0x0879e9cc8f at CoUninitializeEE in mscorwks.dll 
#26 0x0879e9cc2b at CoUninitializeEE in mscorwks.dll 
#27 0x0879e9cb51 at CoUninitializeEE in mscorwks.dll 
#28 0x0879e9ccdd at CoUninitializeEE in mscorwks.dll 
#29 0x0879f88128 at GetPrivateContextsPerfCounters in mscorwks.dll 
#30 0x0879f88202 at GetPrivateContextsPerfCounters in mscorwks.dll 
#31 0x0879f0e255 at InstallCustomModule in mscorwks.dll 
#32 0x087c80b729 at GetModuleFileNameA in KERNEL32.dll 
---------------------------------------------------------------------------- 

我有嘗試各種異常解決問題的方法,最後,我發現如果我的睡眠時間更多毫秒,它會運行更長的時間,但異常仍然是拋出。

希望有人給我如何解決問題的答案......非常感謝!

+0

+我遇到了同樣的問題 – Basic 2012-07-08 23:02:08

回答

0

你從哪裏得到例外(我是指行號)。 我想它是在調用wb.ReadyState != WebBrowserReadyState.Complete,因爲您正在從一個線程上的控件檢索一些數據,它與創建控件實例的線程不是同一個線程。 如果是這種情況,那麼你必須從正確的線程調用屬性。

我會改變你,而循環到這一點:

System.Windows.Forms.WebBrowserReadyState state = GetState(Program.BrowsingSystem.BrowserLink); 
     while (state != System.Windows.Forms.WebBrowserReadyState.Complete) 
     { 
      Application.DoEvents(); 
      Thread.Sleep(new Random().Next(10, 100)); 
     } 

它具有以下調用的方法相關聯:

private System.Windows.Forms.WebBrowserReadyState GetState(System.Windows.Forms.WebBrowser instance) 
    { 
     if (instance.InvokeRequired) 
     { 
      var wbFunc = new Func<System.Windows.Forms.WebBrowser, System.Windows.Forms.WebBrowserReadyState>(RetrieveState); 
      IAsyncResult FunFuncResult = wbFunc.BeginInvoke(instance, null, null); 
      return wbFunc.EndInvoke(FunFuncResult); 

     } 
     else 
     { 
      return RetrieveState(instance); 
     } 
    } 

    public System.Windows.Forms.WebBrowserReadyState RetrieveState(System.Windows.Forms.WebBrowser instance) 
    { 
     return instance.ReadyState; 
    } 

我無法測試它,因爲我錯過了應用程序的其餘部分,但它應該接近最終解決方案。

相關問題