2016-09-17 104 views
0

我已經構建了一個程序來檢查應用程序是否啓用。如果啓用,它將執行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沒有改變,如果達到這部分代碼就一定是「狀態:檢查「。

我的代碼有什麼問題?再次,它在我的電腦中工作,但對其他人來說,它不是。任何幫助是極大的讚賞!

回答

1

按照MSDN,

你一定要小心,不要操縱 你的DoWork的事件處理程序的任何用戶界面對象。而是通過BackgroundWorker事件與用戶界面 通信。

可能是第一行導致錯誤。最好在UI線程上調用它。見How to: Make Thread-Safe Calls to Windows Forms Controls

旁註:

  1. 你在DoWork循環看起來不正確。你正在尋找一行標誌並設置啓用和禁用狀態,如果多行有標誌設置或未設置,該怎麼辦?如果查詢每次只返回1行,那麼你不需要循環這裏
  2. 你應該在DoWork事件處理程序中有一些日誌記錄,以便你知道里面實際發生了什麼。
  3. 通常我們不會在DoWork事件處理程序中處理異常,而是使用RunWorkerCompleted事件來告訴您是否在該過程中發生了任何異常。無論如何,使用這個事件是爲了知道這個過程已經完成是一個好主意。
+0

所以應的代碼是什麼呢?我不知道該怎麼做。該代碼完美地運行我的電腦,但打開到另一個不會執行backgroundworker。我很困惑 –

+0

你檢查鏈接了嗎? – sallushan

+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 

感謝@sallushan

相關問題