加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

wpf – UI无法快速更新

发布时间:2020-12-20 11:18:46 所属栏目:Python 来源:网络整理
导读:晚上好, 以下是我用于从驱动器等读取文件和文件夹的代码. 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 BKW
晚上好,

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

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,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线程无法跟上它的速度,请你就我如何解决这个问题向我提出建议.

解决方法

一个快速的答案是仅报告在一定时间内通过该方式的进度,如果在该时间内处理了10个文件,则UI不会尝试更新每个文件.如果事情处理得那么快,那么你真的不需要在每个文件上更新用户.

另外还要快速注意一下,如果ProgressBar实际上没有报告从0到100%的进度,您可能只想将其IsIndeterminate属性设置为true而不是增加百分比,然后将其重置为0.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读