Wlan在页面加载后连接

时间:2014-01-09 15:44:54

标签: vb.net

我正在尝试在Windows窗体中的Web浏览器组件中加载网页。

在加载页面之前,我运行批处理文件以连接到选定的Wlan连接。

问题是Wlan在一段延迟后连接,并且此时Web浏览器加载了无法加载的网页。

如何在这两个事件之间添加延迟

pe.WaitForExit()

webBrowser1.Navigate("http://10.10.116.5:8090/httpclient.html") 

以下是DocumentCompleted事件

Sub WebBrowser1DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
        Do While webBrowser1.ReadyState <> WebBrowserReadyState.Complete

            MsgBox("Here3")
            webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("username").Item(0).SetAttribute("value",username)
            webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("password").Item(0).SetAttribute("value",password)

            webBrowser1.Document.GetElementById("logincaption").Focus()
            webBrowser1.Document.GetElementById("logincaption").InvokeMember("click")
            MsgBox("logged in")
        Loop
    End Sub

有人可以帮助我

1 个答案:

答案 0 :(得分:1)

您可以尝试在BackgroundWorker中检查批处理文件的“状态”,并在工作人员完成后加载页面。

Public Sub Start()
    Dim file As String = "path_and_name_of_file"
    'TODO: Execute batch "path_and_name_of_file".
    Me.BackgroundWorker1.RunWorkerAsync(file)
End Sub

Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork

    Dim path As String = CStr(e.Argument)
    Dim watch As New Stopwatch()
    Dim timedOut As Boolean = False

    watch.Start()

    Do

        Try
            Using stream As IO.Stream = New IO.FileStream(path, IO.FileMode.Open)
            End Using
            Exit Do
        Catch ex As Exception
        End Try

        'Time out after 2 minutes.
        If (watch.Elapsed.TotalMinutes > 2) Then
            timedOut = True
            Exit Do
        Else
            Threading.Thread.Sleep(500) '<- Give thread some rest.
        End If

    Loop

    watch.Stop()

    ''Uncomment this line if you will add a 3 second delay AFTER executed batch:
    'If (Not cancelled) Then
    '    Threading.Thread.Sleep(3000)
    'End If

End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    'TODO: Load page.
End Sub

修改

想想看,你可以尝试ping ip-address:

Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork

    Dim watch As New Stopwatch()
    Dim timedOut As Boolean = False

    watch.Start()

    Do

        Try
            If (My.Computer.Network.Ping("10.10.116.5", 100)) Then
                Exit Do
            End If
        Catch ex As Exception
        End Try

        'Time out after 2 minutes.
        If (watch.Elapsed.TotalMinutes > 2) Then
            timedOut = True
            Exit Do
        Else
            Threading.Thread.Sleep(500) '<- Give thread some rest.
        End If

    Loop

    watch.Stop()

End Sub