Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net ProgressBar未使用For Each循环更新_Vb.net_Progress Bar - Fatal编程技术网

Vb.net ProgressBar未使用For Each循环更新

Vb.net ProgressBar未使用For Each循环更新,vb.net,progress-bar,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, Mess

我正在将所有.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
,它将在每次复制文件时计数,因此我在每个
循环的
之前添加了此代码:

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
Copier_ProgressBar.Value += 1
并在每个
循环的
中添加此代码:

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
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没有更新

相反,您应该研究如何使用报告来报告进度。像这样的事情应该可以做到:

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

作为补充说明,对于最佳实践,我将考虑使用而不是将字符串串在一起:

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

@Bugs:在某种程度上,你的回答确实有助于我理解后台工作人员是如何工作的,但不幸的是,你的例子根本不适合我。我不是说那是错的。。。也许有什么东西不见了,或者我不见了!!!我正在测试一些东西。。。这就是为什么我还没有给出任何答案。真的很感谢你的时间,无论如何!!!嗯,奇怪的是,我在发布一些示例文件之前做了测试,它确实起了作用。您能否调试并查看是否调用了
BackgroundWorker1\u DoWork
方法?很容易做到;在此处放置断点
BackgroundWorker1.RunWorkerAsync()
,然后按F11单步执行代码。它应该进入
BackgroundWorker1\u-dowwork
。这可能是一个错误吗?我把你的代码用在一个新的空白项目中,工作得很好。因此,我在主项目中的代码有问题。非常感谢你的帮助,我的朋友!!!没问题,希望您找出问题的原因。