【问题标题】:ProgressBar not updating using a For Each loopProgressBar 未使用 For Each 循环进行更新
【发布时间】:2017-04-03 12:34:46
【问题描述】:

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

【问题讨论】:

  • @Bugs :在某种程度上,您的回答确实有助于让我了解 BackgroundWorker 如何工作,但不幸的是,您的示例根本不适合我。我并不是说那是错误的......也许缺少某些东西或我缺少某些东西!我正在测试一些东西......这就是为什么我还没有给出任何答案。无论如何,真的感谢您的宝贵时间!!!
  • 嗯,奇怪,我在发布一些示例文件之前进行了测试,它确实有效。能不能调试一下BackgroundWorker1_DoWork方法是否被调用?很容易做到;在BackgroundWorker1.RunWorkerAsync() 此处放置一个断点,然后按 F11 单步执行代码。它应该进入BackgroundWorker1_DoWork。那里可能有错误?
  • 我将您的代码用于一个新的空白项目并且运行良好。所以,我在主项目中的代码出了点问题。非常感谢你帮助我的朋友!!!
  • 不是问题,希望您找出导致问题的原因。

标签: vb.net progress-bar


【解决方案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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    • 2013-10-15
    • 2014-03-15
    相关资源
    最近更新 更多