BackgroundWorker not Running when it's needed

主宰稳场 提交于 2019-12-13 10:31:36

问题


I am writing a program which lets users upload a large file, compare it to another large file uploaded before and return a list of new entries and discontinued entries.

This requires the program to run a few queries, so it takes a while for the program to complete the task.

Of course, this means that until the program is done with the task, the user cannot do anything else. To prevent that from happening I have included a BackgroundWorker to the project.

The problem is, the BackgroundWorker doesn't start, giving me the same problem.

Can you please help me with this problem? Thanks!

Code:

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:


RunWorkerAsync is, as the name implies, an async method call. This means that the only work done here is to start the DoWork event handler and return immediately. Thus your code now has two paths of execution, the one in the DoWork event handler (that has just started its work and it has probably done nothing) and the code that follows the call to RunWorkerAsync (the code in the ButtonClick).

This last code shows a messagebox and then call CancelAsync, and you could imagine what this call do to your DoWork thread of execution.

So you just need to wait before displaying anything and the Completed event is exactly what you need to use to wait the completition of your DoWork event handler

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


来源:https://stackoverflow.com/questions/38162101/backgroundworker-not-running-when-its-needed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!