ProgressBar不使用For Each循环进行更新

时间:2017-04-03 12:34:47

标签: vb.net progress-bar

我将所有.avi和.png文件从一个文件夹复制到另一个文件夹中:

Private Sub CopierSend_Button_Click(sender As Object, e As EventArgs) Handles CopierSend_Button.Click

    If MessageBox.Show("Click OK to send media or just Cancel.", "Media Copier", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = DialogResult.OK Then
        For Each foundFile As String In My.Computer.FileSystem.GetFiles(FrapsFolder_, Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly, "*.avi", "*.png")
            Dim foundFileInfo As New System.IO.FileInfo(foundFile)
            My.Computer.FileSystem.CopyFile(foundFile, DestFolder_ & foundFileInfo.Name, Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs, Microsoft.VisualBasic.FileIO.UICancelOption.DoNothing)
        Next
    Else
        'Nothing Yet
    End If

End Sub

我想添加ProgressBar,每次复制文件时都会对其进行计数,因此我在For Each循环之前添加了此代码:

Dim file1() As String = IO.Directory.GetFiles(FrapsFolder_, "*.avi")
Dim file2() As String = IO.Directory.GetFiles(FrapsFolder_, "*.png")
Dim files = file1.Length + file2.Length
Copier_ProgressBar.Step = 1
Copier_ProgressBar.Maximum = files

并在For Each循环中添加了此代码:

Copier_ProgressBar.Value += 1

以下是我的所有代码:

Private Sub CopierSend_Button_Click(sender As Object, e As EventArgs) Handles CopierSend_Button.Click

    If MessageBox.Show("Click OK to send media or just Cancel.", "Media Copier", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = DialogResult.OK Then

        Dim file1() As String = IO.Directory.GetFiles(FrapsFolder_, "*.avi")
        Dim file2() As String = IO.Directory.GetFiles(FrapsFolder_, "*.png")
        Dim files = file1.Length + file2.Length
        Copier_ProgressBar.Step = 1
        Copier_ProgressBar.Maximum = files

        For Each foundFile As String In My.Computer.FileSystem.GetFiles(FrapsFolder_, Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly, "*.avi", "*.png")
            Dim foundFileInfo As New System.IO.FileInfo(foundFile)
            My.Computer.FileSystem.CopyFile(foundFile, DestFolder_ & foundFileInfo.Name, Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs, Microsoft.VisualBasic.FileIO.UICancelOption.DoNothing)
            Copier_ProgressBar.Value += 1
        Next

    Else

        'Nothing Yet

    End If
End Sub

ProgressBar更新,但仅在复制了所有文件而不是实时更新之后。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

目前看来ProgressBar似乎没有做任何事情,直到所有文件复制后 。这不是严格正确的。相反,您的UI不会更新。

相反,您应该考虑使用BackgroundWoker来报告进度。这样的事情可以解决问题:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    'This can also be set using the Designer
    BackgroundWorker1.WorkerReportsProgress = True

End Sub

Private Sub CopierSend_Button_Click(sender As Object, e As EventArgs) Handles CopierSend_Button.Click

    If MessageBox.Show("Click OK to send media or just Cancel.", "Media Copier", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = DialogResult.OK Then
        Dim file1() As String = IO.Directory.GetFiles(FrapsFolder_, "*.avi")
        Dim file2() As String = IO.Directory.GetFiles(FrapsFolder_, "*.png")
        Dim files As Integer = file1.Length + file2.Length

        Copier_ProgressBar.Step = 1
        Copier_ProgressBar.Maximum = files       

        BackgroundWorker1.RunWorkerAsync()
    End If

End Sub

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

    For Each foundFile As String In My.Computer.FileSystem.GetFiles(FrapsFolder_, Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly, "*.avi", "*.png")
        Dim foundFileInfo As New System.IO.FileInfo(foundFile)
        My.Computer.FileSystem.CopyFile(foundFile, DestFolder_ & foundFileInfo.Name, Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs, Microsoft.VisualBasic.FileIO.UICancelOption.DoNothing)
        BackgroundWorker1.ReportProgress(Copier_ProgressBar.Value + 1)
    Next

End Sub

Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged

    Copier_ProgressBar.Value = e.ProgressPercentage

End Sub

作为补充说明,为了获得最佳实践,我会考虑使用Path.Combine而不是将字符串连接在一起:

My.Computer.FileSystem.CopyFile(foundFile, Path.Combine(DestFolder_, foundFileInfo.Name), Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs, Microsoft.VisualBasic.FileIO.UICancelOption.DoNothing)