2014-09-28 55 views
2

我有這樣的代碼,只需導航到一個網站web瀏覽器的執行流

class BrowsingUrl { 
    private System.Windows.Forms.WebBrowser nBrowser ; 
     private System.Windows.Forms.Form theFormLayout1; 

    public BrowsingUrl(System.Windows.Forms.Form theFormLayout) { 

      this.nBrowser = new System.Windows.Forms.WebBrowser(); 
      this.nBrowser.Dock = System.Windows.Forms.DockStyle.Fill; 
      this.nBrowser.Location = new System.Drawing.Point(0, 0); 
      this.nBrowser.MinimumSize = new System.Drawing.Size(20, 20); 
      this.nBrowser.Name = "webBrowser1"; 
      this.nBrowser.ScrollBarsEnabled = false; 
      this.nBrowser.Size = new System.Drawing.Size(1300, 700); 
      this.nBrowser.TabIndex = 0; 
      this.theFormLayout1 = theFormLayout; 
      this.theFormLayout1.Controls.Add(this.nBrowser); 
      this.nBrowser.Navigate("https://stackoverflow.com"); 
      this.nBrowser.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.nBrowser_DocumentCompleted); 
    } 

    private void nBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
    { 

     // do stuff 
    } 
}  

如果創建一個browsingurl反對一切工作正常,但如果創建了兩個對象,似乎是這兩個構造函數運行在同一時間我怎麼才能讓第二個對象等待,直到第一個終點執行

public partial class Form1 : Form 
{ 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void startbtn_Click(object sender, EventArgs e) 
     { 
      BrowsingUrl BrowsingUrl1 = new BrowsingUrl(this); 
      //wait till browsingUrl1 finish 
      BrowsingUrl BrowsingUrl2 = new BrowsingUrl(this); 
     } 
}  
+0

可能重複http://stackoverflow.com/questions/583897/c-sharp-how-to-a-web-to-finish-loading-before-continued) – 2014-09-28 15:47:53

+0

不幸的是不是同一個問題 – magicalll 2014-09-28 15:52:13

+0

我想@magicalll想等待第一個'nBrowser_DocumentCompleted'在開始第二個構造函數之前完成。 – kennyzx 2014-09-28 15:53:16

回答

0

nBrowser_DocumentCompleted_isLoaded值設置爲true並使其作爲一個公共屬性進行訪問。還加ScriptErrorsSuppressed = true

class BrowsingUrl 
    { 
     private System.Windows.Forms.WebBrowser nBrowser; 
     private System.Windows.Forms.Form theFormLayout1; 
     private bool _isLoaded = false; 
     public bool IsLoaded 
     { 
      get { return _isLoaded && !nBrowser.IsBusy && 
        nBrowser.ReadyState == WebBrowserReadyState.Complete; } 
     } 
     public BrowsingUrl(System.Windows.Forms.Form theFormLayout) 
     { 

      this.nBrowser = new System.Windows.Forms.WebBrowser(); 
      this.nBrowser.Dock = System.Windows.Forms.DockStyle.Fill; 
      this.nBrowser.Location = new System.Drawing.Point(0, 0); 
      this.nBrowser.MinimumSize = new System.Drawing.Size(20, 20); 
      this.nBrowser.Name = "webBrowser1"; 
      this.nBrowser.ScrollBarsEnabled = false; 
      this.nBrowser.Size = new System.Drawing.Size(1300, 700); 
      this.nBrowser.TabIndex = 0; 
      this.theFormLayout1 = theFormLayout; 
      this.theFormLayout1.Controls.Add(this.nBrowser); 
      this.nBrowser.ScriptErrorsSuppressed = true; 
      this.nBrowser.Navigate("https://stackoverflow.com"); 
      this.nBrowser.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.nBrowser_DocumentCompleted); 
     } 
     private void nBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
     { 
      //check for frames 
      if (e.Url.AbsolutePath != (sender as WebBrowser).Url.AbsolutePath) 
       return; 

      // do stuff 
      //Sample do stuff 
      Thread.Sleep(10000); 
      // end do stuff 

      _isLoaded = true; 
     } 
    } 

然後你就可以等到BrowsingUrl1加載:

 BrowsingUrl BrowsingUrl1 = new BrowsingUrl(this); 
     //Wait for the first page to load 
     while (!BrowsingUrl1.IsLoaded) 
     { 
      Application.DoEvents(); 
     } 
     BrowsingUrl BrowsingUrl2 = new BrowsingUrl(this); 
的[C#如何等待一個網頁,然後再繼續完成加載(
+0

謝謝先生您的迴應,我試過這個解決方案,沒有運氣兩個實例都在運行,但是這個解決方案讓我清楚這兩個實例是在兩個獨立的線程, – magicalll 2014-09-28 16:36:52

+0

是的,我現在看到它,它不適用於某些頁面。問題在於JavaScript在頁面內異步執行。我會嘗試找到其他方式。 – 2014-09-28 17:01:14

+0

@magicalll更新了答案,經過測試,似乎工作正常。但是也許你的問題可能是你想要訪問加載的文檔中的一些HTML('do stuff'),並且你想要修改該HTML。這將是一個不同的問題。 – 2014-09-28 18:18:26