我创建了 2 个 BackgroundWorker,而我的第二个 BackgroundWorker 似乎根本不起作用,我在“Private Sub BackgroundWorker2_DoWork”下放置了一个消息框指示器,它被触发了,但它下面的代码没有。这是我的 BackgroundWorker 有问题的完整代码。有什么导致这种情况的吗?
我的程序确实需要 2 个 BackgroundWorker,因为它正在处理大量文件,这会使应用程序挂起。
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
If BackgroundWorker2.IsBusy <> True Then
BackgroundWorker2.RunWorkerAsync()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub BackgroundWorker2_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
Dim worker1 As System.ComponentModel.BackgroundWorker = CType(sender, System.ComponentModel.BackgroundWorker)
Try
MessageBox.Show("the program was able to open me")
'this message box above was able to display but the codes below were not processed
Dim Stream As System.IO.FileStream
Dim Index As Integer = 0
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "D:\work\base tremble"
openFileDialog1.Filter = "txt files (*.txt)|*.txt"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
'This line opens the file the user selected and sets the stream object
Stream = openFileDialog1.OpenFile()
If (Stream IsNot Nothing) Then
'create the reader here and use the stream you got from the file open dialog
Dim sReader As New System.IO.StreamReader(Stream)
Do While sReader.Peek >= 0
ReDim Preserve eArray(Index)
eArray(Index) = sReader.ReadLine
RichTextBox3.Text = eArray(Index)
Index += 1
worker1.ReportProgress(Index)
'Delay(2)
Loop
Label1.Text = "0/" & eArray.Length & ""
End If
Catch Ex As Exception
MessageBox.Show(Ex.Message)
Finally
If (Stream IsNot Nothing) Then
Stream.Close()
End If
End Try
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub BackgroundWorker2_ProgressChanged(sender As System.Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker2.ProgressChanged
Try
'Label1.Text = e.ProgressPercentage.ToString()
Me.ProgressBar2.Value = e.ProgressPercentage
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub BackgroundWorker2_Completed(sender As System.Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker2.RunWorkerCompleted
Try
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub