none
迴圈等裡面等待網頁無效 RRS feed

  • 問題

  •   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Object
        Me.Text = "狀態 - 初始化中..."
        'Button1.Enabled = False
        'ListBox1.Enabled = False
        For i = 0 To ListBox1.Items.Count - 1
          Me.Text = "狀態 - " & i + 1 & " of " & ListBox1.Items.Count
          ListBox1.SelectedIndex = i
          Dim strUrl As String = ListBox1.Items(i).ToString 'VB6.GetItemString(ListBox1, i)
          WebBrowser1.Navigate(strUrl)
          Do While WebBrowser1.IsBusy
            Application.DoEvents()
          Loop
          'gip()
        Next
        'Button1.Enabled = True
        'ListBox1.Enabled = True
        MsgBox("完成!", MsgBoxStyle.Information, "完成")
      End Sub
    

    有沒有比較好的方法

    因為我是讀取ListBox裡面的網址

    測試中遇到網頁還沒開啟迴圈就一直跑

    2010年8月29日 上午 06:52

解答

  • 
    
     Dim intIndex As Integer = 0
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
     intIndex = 0
     ListBox1.SelectedIndex = intIndex 
     Dim strUrl As String = ListBox1.Items(intIndex ).ToString
     WebBrowser1.Navigate(strUrl)
    
     End Sub
    
    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
     html = WebBrowser1.DocumentText '獲取到的html.
     intIndex +=1
      If intIndex <= ListBox1.Items.Count - 1
    ListBox1.SelectedIndex = intIndex  
    Dim strUrl As String = ListBox1.Items(intIndex ).ToString  
    WebBrowser1.Navigate(strUrl)  
    End If 
    End Sub

    • 已標示為解答 ahwa1 2010年8月29日 下午 04:43
    2010年8月29日 上午 11:57

所有回覆

  • WebBrowser.DocumentCompleted 事件

    http://msdn.microsoft.com/zh-tw/library/system.windows.forms.webbrowser.documentcompleted(VS.80).aspx

    改寫在DocumentCompleted事件裡,宣告全域變數記錄ListBox的index,在事件裡index+1,每當觸發事件時,才開啟新網頁...

    2010年8月29日 上午 07:07
  • 2010年8月29日 上午 07:12
    版主
  • 參考了大陸的
    
    <a href="http://www.csharpwin.com/csharpspace/8838r6697.shtml">http://www.csharpwin.com/csharpspace/8838r6697.shtml</a>
    
    不過改善了80% 因為這是可以等待的 不過等待完畢要做循環會跳兩個ListBox裡面的網址才等待
    
    是不是還要另外處理呢
    
      Private loading As Boolean = True '該變量表示網頁是否正在加載.
      Private html As String = String.Empty
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        Me.Text = "狀態 - 初始化中..."
        'Button1.Enabled = False
        'ListBox1.Enabled = False
        'browser.Navigated += New WebBrowserNavigatedEventHandler(AddressOf WebBrowser1_Navigated)
        For i = 0 To ListBox1.Items.Count - 1
          Me.Text = "狀態 - " & i + 1 & " of " & ListBox1.Items.Count
          ListBox1.SelectedIndex = i
          Dim strUrl As String = ListBox1.Items(i).ToString
          loading = True '表示正在加載
          WebBrowser1.Navigate(strUrl)
          While loading '等待本次加載完畢才執行下次循環.
            Application.DoEvents()
          End While
          'gip()
        Next
        'Button1.Enabled = True
        'ListBox1.Enabled = True
        MsgBox("完成!", MsgBoxStyle.Information, "完成")
      End Sub
    
    
    
      Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        html = WebBrowser1.DocumentText '獲取到的html.
        loading = False '在加載完成後,將該變量置為false,下一次循環隨即開始執行.
        'WebBrowser1.Document.Window.ScrollTo(500, 500)
    
        'Dim body As HtmlElement = WebBrowser1.Document.GetElementsByTagName("body").Item(0)
        'AddHandler body.MouseMove, AddressOf WebMousePosition
      End Sub
    
    2010年8月29日 上午 08:05
  • 在事件裡index+1 這個要怎麼弄呢
    2010年8月29日 上午 11:21
  • 
    
     Dim intIndex As Integer = 0
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
     intIndex = 0
     ListBox1.SelectedIndex = intIndex 
     Dim strUrl As String = ListBox1.Items(intIndex ).ToString
     WebBrowser1.Navigate(strUrl)
    
     End Sub
    
    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
     html = WebBrowser1.DocumentText '獲取到的html.
     intIndex +=1
      If intIndex <= ListBox1.Items.Count - 1
    ListBox1.SelectedIndex = intIndex  
    Dim strUrl As String = ListBox1.Items(intIndex ).ToString  
    WebBrowser1.Navigate(strUrl)  
    End If 
    End Sub

    • 已標示為解答 ahwa1 2010年8月29日 下午 04:43
    2010年8月29日 上午 11:57