我已经构建了一个程序,用于检查应用程序是否已启用。如果启用,它将执行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", "System", MessageBoxButtons.OK)
Close()
Else
Status = ""
End If
End If
End Sub
对我而言,我认为它并没有让BackgroundWorker
开始工作。我已经通过在第一行代码之后放置MsgBox("Code 1 Success")
而在第二行代码之后放置MsgBox("Code 2 Success")
来测试代码,依此类推。并且它到达BackgroundWorker1.RunWorkerAsync()
代码,但它不执行BackgroundWorker下的代码。
这是BackgroundWorker1代码:
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Label1.Text = "Status: Checking"
Label1.ForeColor = Color.FromKnownColor(KnownColor.Highlight)
Try
Dim mysqlconnection As MySqlConnection = New MySqlConnection("server=85.10.205.173;port=3306;username='" & User & "';password='" & Pass & "'")
Dim mysqlcommand As MySqlCommand = Nothing
Dim mysqldatareader As MySqlDataReader = Nothing
mysqlconnection.Open()
Using table As DataTable = New DataTable
Using command As MySqlCommand = New MySqlCommand("Select * from login.accounts where Username = 'Jake';", mysqlconnection)
Using adapter As MySqlDataAdapter = New MySqlDataAdapter(command)
adapter.Fill(table)
End Using
End Using
For Each row As DataRow In table.Rows
If row("Flag") = "enable" Then
Label1.Text = "Status: Enabled"
Label1.ForeColor = Color.Green
Button1.Enabled = False
Button2.Enabled = True
ProgressBar1.Visible = False
Else
Label1.Text = "Status: Disabled"
Label1.ForeColor = Color.OrangeRed
Button1.Enabled = True
Button2.Enabled = False
ProgressBar2.Visible = False
End If
Next
End Using
mysqlconnection.Close()
Catch ex As Exception
Threading.Thread.Sleep(1000)
Label1.Text = "No Internet Connection"
End Try
End Sub
我知道它没有达到此代码,因为Label1.Text
没有改变,如果它到达代码的这一部分,那一定是“状态:正在检查”。
我的代码有什么问题?同样,它适用于我的计算机,但对其他人而言,它没有。非常感谢任何帮助!
答案 0 :(得分:1)
根据MSDN,
您必须小心不要操纵任何用户界面对象 你的DoWork事件处理程序。而是与用户界面进行通信 通过BackgroundWorker事件。
可能第一行导致错误。最好在UI线程上调用它。见How to: Make Thread-Safe Calls to Windows Forms Controls
附注:
DoWork
中的循环似乎不对。您正在寻找行中的标志并设置启用和禁用状态,如果多行已设置标志或未设置,该怎么办?如果查询每次只返回1行,那么您不需要这里的循环DoWork
事件处理程序中进行一些登录,以便了解内部实际发生的情况。DoWork
事件处理程序中处理异常,而是使用RunWorkerCompleted
事件来告诉您进程中是否发生了异常。无论如何,最好使用此事件以了解该过程已完成。答案 1 :(得分: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
谢谢@sallushan