我的第二个BackgroundWorker不起作用

时间:2013-07-10 19:03:42

标签: vb.net backgroundworker

我已经创建了2个BackgroundWorkers,而我的第二个BackgroundWorker似乎根本不起作用,我在“Private Sub BackgroundWorker2_DoWork”下放置了一个消息框指示符并且它已被触发但是其下的代码不是。这是我的BackgroundWorker的完整代码,它有问题。有什么东西导致了这个吗?

我的程序确实需要2个BackgroundWorkers,因为它正在处理大量文件,导致应用程序挂起。

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

1 个答案:

答案 0 :(得分:1)

我已将OpenFileDialog编入一个不同的按钮,我将后续存储的数据处理到后台工作者的数组中,现在可以正常工作。

感谢关于OpenFileDialog的不断提及的背景工作,它给了我一个提示。