ProgressBar 未使用 For Each 循环更新

ProgressBar not updating using a For Each loop

我正在将一个文件夹中的所有 .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 更新,但只有在所有文件都被复制后才会更新,而不是实时更新。有什么想法吗?

目前看起来 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)