2011-05-13 46 views
3

晚上好,UI没有更新快

以下是我用于从驱动器读取的文件和文件夹代码等

Public Class LoadingBox 

    Public counter As ULong 
    Public OpenRecords As New Dictionary(Of String, MainWindow.records) 
    Public Path As String 
    Public Diskname As String 
    Private WithEvents BKWorker As New BackgroundWorker() 


    Public Sub New(ByVal _Path As String, ByVal _Diskname As String) 

     ' This call is required by the designer. 
     InitializeComponent() 

     ' Add any initialization after the InitializeComponent() call. 
     Path = _path 
     Diskname = _diskname 
    End Sub 

    Private Sub GetStructure(ByVal tempdir As String, ByVal ParentID As String, ByVal DiskName As String) 
     Dim maindir As DirectoryInfo = My.Computer.FileSystem.GetDirectoryInfo(tempdir) 
     For Each Dir As DirectoryInfo In maindir.GetDirectories 
      Try 
       Dim d As New MainWindow.records 
       d.Filename = Dir.Name 
       d.Folder = True 
       d.Rowid = Date.UtcNow.ToString() + counter.ToString() 
       d.Size = 0 
       d.ParentID = ParentID 
       d.DiskName = DiskName 
       d.DateCreated = Dir.CreationTimeUtc 
       d.DateModified = Dir.LastWriteTimeUtc 
       OpenRecords.Add(d.Rowid, d) 
       'Label1.Content = "Processing: " + Dir.FullName 
       BKWorker.ReportProgress(0, Dir.FullName) 
       counter = counter + 1 
       GetStructure(Dir.FullName, d.Rowid, DiskName) 
      Catch ex As Exception 

      End Try 


     Next 
     For Each fil As FileInfo In maindir.GetFiles 
      Try 
       Dim d As New MainWindow.records 
       d.Filename = fil.Name 
       d.Folder = False 
       d.Rowid = Date.UtcNow.ToString() + counter.ToString() 
       d.Size = fil.Length 
       d.ParentID = ParentID 
       d.DiskName = DiskName 
       d.DateCreated = fil.CreationTimeUtc 
       d.DateModified = fil.LastWriteTimeUtc 
       OpenRecords.Add(d.Rowid, d) 
       'Label1.Content = "Processing: " + fil.FullName 
       BKWorker.ReportProgress(0, fil.FullName) 
       counter = counter + 1 
      Catch ex As Exception 

      End Try 

     Next 
    End Sub 

    Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded 
     counter = 0 
     BKWorker.WorkerReportsProgress = True 
     AddHandler BKWorker.DoWork, AddressOf BKWorker_Do 
     AddHandler BKWorker.ProgressChanged, AddressOf BKWorker_Progress 
     AddHandler BKWorker.RunWorkerCompleted, AddressOf BKWorker_Completed 
     BKWorker.RunWorkerAsync() 

     'GetStructure(Path, "0", Diskname) 
    End Sub 

    Private Sub BKWorker_Do(ByVal sender As Object, ByVal e As DoWorkEventArgs) 
     'Throw New NotImplementedException 
     GetStructure(Path, "0", Diskname) 
    End Sub 

    Private Sub BKWorker_Progress(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) 
     'Throw New NotImplementedException 
     Label1.Content = "Processing: " + e.UserState.ToString() 
     If ProgressBar1.Value = 100 Then 
      ProgressBar1.Value = 0 
     End If 
     ProgressBar1.Value = ProgressBar1.Value + 1 
    End Sub 

    Private Sub BKWorker_Completed(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) 
     'Throw New NotImplementedException 
     MessageBox.Show("Completed") 
     Me.Close() 
    End Sub 
End Class 

然而问题是,后台线程能够阅读文件速度非常快,但UI线程无法跟上速度,请问我可以如何解决这个问题。

回答

3

一个简单的答案是,如果在这段时间内处理了10个文件,UI没有试图每次更新一个文件,那么只有在某个时间段已经过去了的时候才报告进度。如果事情处理的速度很快,那么你真的不需要在每一个文件上更新用户。

在边注

此外,如果你的进度实际上并没有报告从0到你可能只想其IsIndeterminate属性设置为true,而不是增加的百分比,然后重置回0

的100%,进展
+0

非常感谢您的快速回复。通过遵循你的建议解决,也注意到IsIndeterminate,感谢信息。 – surpavan 2011-05-13 19:07:53

+0

25个文件对我最有效。感谢:D。 – surpavan 2011-05-13 19:17:09

4

当您迭代那么多项目时,您几乎都不想在每一件物品上报告进度。

我建议在报告进度之前找到一些合理数量的文件等待。每5或10日左右。你可能想看看你的正常数量的文件是什么。换句话说,如果你通常只处理25个文件,你可能不希望只更新每10个文件。但是如果你正在处理25000个文件,你甚至可能只更新每100个文件。

+0

非常感谢您的快速回复。遵循你的建议解决。 – surpavan 2011-05-13 19:06:25

+0

25个文件对我最有效。感谢:D。 – surpavan 2011-05-13 19:16:55