【问题标题】:How to add BackgroundWorker for this code?如何为此代码添加 BackgroundWorker?
【发布时间】:2013-10-25 21:42:24
【问题描述】:

我有以下代码,它是将外部日志文件解析为 datagridview 的代码,但是当我加载大文件时,操作需要时间并且表单会冻结一点,我需要的是显示进度bar 在解析日志文件时。

这是代码:

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            Using Reader As New Microsoft.VisualBasic.FileIO.
        TextFieldParser(TextBox1.Text)
                Reader.TextFieldType =
                            Microsoft.VisualBasic.FileIO.FieldType.FixedWidth

                Reader.SetFieldWidths(Convert.ToInt32(txtDate.Text), Convert.ToInt32(txtTime.Text), Convert.ToInt32(txtExt.Text), Convert.ToInt32(txtCO.Text), _
                                     Convert.ToInt32(txtNumber.Text), Convert.ToInt32(txtDuration.Text), Convert.ToInt32(txtAccCode.Text))
                Dim currentRow As String()
                While Not Reader.EndOfData
                    Try
                        currentRow = Reader.ReadFields()
                        Dim currentField As String

                        For Each currentField In currentRow
                            Dim curRowIndex = dg1.Rows.Add()
                            ' Set the first cell of the new row....
                            dg1.Rows(curRowIndex).Cells(0).Value = currentRow(0)
                            dg1.Rows(curRowIndex).Cells(1).Value = currentRow(1)
                            dg1.Rows(curRowIndex).Cells(2).Value = currentRow(2)
                            dg1.Rows(curRowIndex).Cells(3).Value = currentRow(3)
                            dg1.Rows(curRowIndex).Cells(4).Value = currentRow(4)
                            dg1.Rows(curRowIndex).Cells(5).Value = currentRow(5)
                            dg1.Rows(curRowIndex).Cells(6).Value = currentRow(6)
                        Next

                    Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                        MsgBox("Line " & ex.Message &
                        "is not valid and will be skipped.")
                    End Try
                End While
                MsgBox("Total records imported : " & dg1.RowCount)
                lblTotal.Text = "Total Records: " & dg1.RowCount

            End Using
        Catch
            MsgBox("Invalid file, please make sure you entered the right path")
        End Try

    End Sub

【问题讨论】:

    标签: vb.net datagridview backgroundworker


    【解决方案1】:

    添加一个 BackgroundWorker 并将其 WorkerReportsProgressProperty 设置为 True。

    添加进度条。

    然后试试这段代码:

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Try
                Dim widths() As Integer = { _
                    Convert.ToInt32(txtDate.Text), Convert.ToInt32(txtTime.Text), Convert.ToInt32(txtExt.Text), _
                    Convert.ToInt32(txtCO.Text), Convert.ToInt32(txtNumber.Text), Convert.ToInt32(txtDuration.Text), _
                    Convert.ToInt32(txtAccCode.Text)}
                ProgressBar1.Visible = True
                ProgressBar1.Style = ProgressBarStyle.Marquee ' continuos animation 
                Dim input As New Tuple(Of String, Integer())(TextBox1.Text, widths)
                BackgroundWorker1.RunWorkerAsync(input)
            Catch ex As Exception
                MessageBox.Show("Invalid Width!")
            End Try
        End Sub
    
        Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            Dim input As Tuple(Of String, Integer()) = DirectCast(e.Argument, Tuple(Of String, Integer()))
            Try
                Using Reader As New Microsoft.VisualBasic.FileIO.TextFieldParser(input.Item1)
                    Reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.FixedWidth
                    Reader.SetFieldWidths(input.Item2)
                    Dim currentRow() As String
                    While Not Reader.EndOfData
                        Try
                            currentRow = Reader.ReadFields()
                            BackgroundWorker1.ReportProgress(-1, New Object() { _
                                currentRow(0), currentRow(1), currentRow(2), _
                                currentRow(3), currentRow(4), currentRow(5), _
                                currentRow(6)})
                        Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                            MessageBox.Show("Line is not valid and will be skipped." & vbCrLf & vbCrLf & ex.Message)
                        End Try
                    End While
                End Using
            Catch
                MsgBox("Invalid file, please make sure you entered the right path")
            End Try
        End Sub
    
        Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
            Dim values() As Object = DirectCast(e.UserState, Object())
            dg1.Rows.Add(values)
        End Sub
    
        Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
            MessageBox.Show("Total records imported : " & dg1.RowCount)
            lblTotal.Text = "Total Records: " & dg1.RowCount
            ProgressBar1.Style = ProgressBarStyle.Continuous
            ProgressBar1.Visible = False
        End Sub
    

    【讨论】:

    • 感谢代码,但它现在只在第一列添加一个字段,值为 System.Object[]
    • 我更新了 ProgressChanged() 事件...看看是否更好。
    猜你喜欢
    • 2015-04-08
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 2012-12-02
    • 2017-06-05
    • 1970-01-01
    相关资源
    最近更新 更多