WebBrowser和BackgroundWorker VB

时间:2016-09-17 14:53:32

标签: vb.net

WebBrowser.DocumentCompleted事件能否正确执行BackgroundWorker.RunWorkerAsync()?因为我的程序似乎没有在BackgroundWorker下执行代码。

代码:

Dim Status As String = ""
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    If Status = "Enabled" Or Status = "Disabled" Then
    Else
        Status = WebBrowser1.Document.GetElementById(Account & "Flag").InnerText.ToString
        If Status = "Enabled" Then
            BackgroundWorker1.RunWorkerAsync()
        ElseIf Status = "Disabled" Then
            MessageBox.Show("disabled. Contact admin for more information.", "JKLorenzo", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Close()
        End If
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

我终于开始工作了

以下是我使用的代码:

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    BackgroundWorker1.ReportProgress(10)
    Dim mysqlconnection As MySqlConnection = New MySqlConnection("server=85.10.205.173;port=3306;username='" & User & "';password='" & Pass & "'")
    BackgroundWorker1.ReportProgress(20)
    Dim mysqlcommand As MySqlCommand = Nothing
    BackgroundWorker1.ReportProgress(30)
    Dim mysqldatareader As MySqlDataReader = Nothing
    BackgroundWorker1.ReportProgress(40)
    mysqlconnection.Open()
    BackgroundWorker1.ReportProgress(50)
    Using table As DataTable = New DataTable
        BackgroundWorker1.ReportProgress(60)
        Using command As MySqlCommand = New MySqlCommand("Select * from my.accounts where Username = 'Ray';", mysqlconnection)
            BackgroundWorker1.ReportProgress(70)
            Using adapter As MySqlDataAdapter = New MySqlDataAdapter(command)
                BackgroundWorker1.ReportProgress(80)
                adapter.Fill(table)
                BackgroundWorker1.ReportProgress(90)
            End Using
        End Using

        For Each row As DataRow In table.Rows
            If row("Flag") = "enable" Then
                e.Result = "1"
                BackgroundWorker1.ReportProgress(100)
            Else
                e.Result = "0"
                BackgroundWorker1.ReportProgress(100)
            End If
        Next
    End Using
    mysqlconnection.Close()
End Sub

Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    If e.ProgressPercentage = 10 Then
        Label1.Text = "Status: Checking"
        Label1.ForeColor = Color.FromKnownColor(KnownColor.Highlight)
    End If
    ProgressBar1.Value = e.ProgressPercentage
    ProgressBar1.Refresh()
End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    Threading.Thread.Sleep(500)
    ProgressBar1.Value = 0
    If e.Result = "1" Then
        Label1.Text = "Status: Enabled"
        Label1.ForeColor = Color.Green
        Button1.Enabled = False
        Button2.Enabled = True
    ElseIf e.Result = "0" Then
        Label1.Text = "Status: Disabled"
        Label1.ForeColor = Color.OrangeRed
        Button1.Enabled = True
        Button2.Enabled = False
    Else
        MessageBox.Show("Unknown output: " & e.Result & " . Closing", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Close()
    End If
End Sub