BackgroundWorker在需要时不运行

时间:2016-07-02 16:17:39

标签: mysql vb.net backgroundworker

我正在编写一个程序,允许用户上传大文件,将其与之前上传的另一个大文件进行比较,并返回新条目和已停止条目的列表。

这需要程序运行一些查询,因此程序完成任务需要一段时间。

当然,这意味着在程序完成任务之前,用户无法做任何其他事情。为了防止这种情况发生,我在项目中加入了一个BackgroundWorker。

问题是,BackgroundWorker无法启动,给我同样的问题。

你可以帮我解决这个问题吗?谢谢!

代码:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim opendialog As New OpenFileDialog
    Dim filepath As String = ""
    Dim cellvalue(20) As String

    opendialog.Title = "Elija el archivo que quiere importar"
    opendialog.Filter = "CSV (*.csv)|*.csv"

    If opendialog.ShowDialog() = DialogResult.Cancel Then
        Exit Sub
    End If

    filepath = Replace(opendialog.FileName, "\", "\\")

    Label1.Visible = True 'This is supposed to appear first, but it doesn't appear until the end of the method.

    'Reading CSV file content 
    Cmd.CommandText = "SELECT COUNT(*) AS cuenta FROM libros WHERE 1"
    rs = Cmd.Execute

    If rs("cuenta").Value = 0 Then
        BackgroundWorker1.RunWorkerAsync()
        'MySQL queries, some of which takes a long time due to the large file being processed

        Beep()
        MsgBox("Archivo exportado con éxito",, "Exito")
        BackgroundWorker1.CancelAsync()

    Else
        BackgroundWorker1.RunWorkerAsync()

        'MySQL queries, some of which takes a long time due to the large file being processed

        Beep()
        MsgBox("Archivo exportado con éxito",, "Exito")
        BackgroundWorker1.CancelAsync()

    End If
End Sub

Private Sub BackgroundWoker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    ' Update the progress bar
    Me.ProgressBar1.Value = e.ProgressPercentage
End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    If e.Cancelled Then
        Label1.Text = "Cancelled"
    Else
        Label2.Text = "Completed"
    End If
End Sub

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object,
                 ByVal e As System.ComponentModel.DoWorkEventArgs) _
                 Handles BackgroundWorker1.DoWork
    ' Do some time-consuming work on this thread.
    System.Threading.Thread.Sleep(5)
End Sub

1 个答案:

答案 0 :(得分:0)

RunWorkerAsync,顾名思义,是一个异步方法调用。这意味着这里完成的唯一工作是启动DoWork事件处理程序并立即返回。因此,您的代码现在有两个执行路径,一个在DoWork事件处理程序中(刚刚开始工作并且可能什么也没做),以及调用RunWorkerAsync之后的代码(ButtonClick中的代码)。

最后一段代码显示了一个消息框,然后调用CancelAsync,你可以想象这个调用会对你的DoWork执行线程做些什么。

所以你只需要在显示任何内容之前等待,并且Completed事件正是你需要用来等待你的DoWork事件处理程序的完整性

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
.....
    BackgroundWorker1.RunWorkerAsync()
    ' Remove these lines....
    ' Beep()
    ' MsgBox("Archivo exportado con éxito",, "Exito")
    ' BackgroundWorker1.CancelAsync()
End Sub

 Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    If e.Cancelled Then
        Label1.Text = "Cancelled"
    Else
        Label2.Text = "Completed"
        ' Add the message here....'
        Beep()
        MsgBox("Archivo exportado con éxito",, "Exito")

        ' Of course at this point you don't need to cancel anything....'
    End If
End Sub

Private Sub BackgroundWoker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    Me.ProgressBar1.Value = e.ProgressPercentage
End Sub

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object,
             ByVal e As System.ComponentModel.DoWorkEventArgs) _
             Handles BackgroundWorker1.DoWork
    ' Now suppose that your work here could be divided in 5 methods'

    ExecuteMethod1()
    backgroundWorker1.ReportProgress(20)   
    ExecuteMethod2)
    backgroundWorker1.ReportProgress(40   
    ExecuteMethod3
    backgroundWorker1.ReportProgress(60)   
    ExecuteMethod4
    backgroundWorker1.ReportProgress(80)   
    ExecuteMethod5
    backgroundWorker1.ReportProgress(100)   

End Sub