How to prevent UI from freezing during lengthy process?

后端 未结 4 1379
慢半拍i
慢半拍i 2021-01-15 06:27

I need to write a VB.Net 2008 applet to go through all the fixed-drives looking for some files. If I put the code in ButtonClick(), the UI freezes until the code is done:

4条回答
  •  隐瞒了意图╮
    2021-01-15 06:41

    The key is to seperate the UI code from the actual functionality code. The time-consuming functionality should run on a seperate thread. To achieve this, you can either:

    1. Create and start a Thread object by yourself
    2. Create a Delegate and use asynchronous invokation (using BeginInvoke).
    3. Create and start a BackgroundWorker.

    As you mentioned, you should avoid Application.DoEvents(). A proper breakdown of the application's functionality will allow you to create an application which is designed to be responsive, rather than creating a non-responsive application with DoEvents "fixes" (which is costly, considered bad practice, and implies a bad design).

    Since your method doesn't return a value and doesn't update the UI, the fastest solution might be creating a Delegate and using "fire and forget" asynchronous invokation:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Call New Action(AddressOf DrivesIteration).BeginInvoke(Nothing, Nothing)
    End Sub
    
    Private Sub DrivesIteration()
        Dim drive As DriveInfo
        Dim filelist As Collections.ObjectModel.ReadOnlyCollection(Of String)
        Dim filepath As String
    
        For Each drive In DriveInfo.GetDrives()
            If drive.DriveType = DriveType.Fixed Then
                filelist = My.Computer.FileSystem.GetFiles(drive.ToString, FileIO.SearchOption.SearchAllSubDirectories, "MyFiles.*")
                For Each filepath In filelist
                    DoStuff(...)
                Next
            End If
        Next 
    End Sub
    

    BTW, For..Next blocks no longer have to end with "Next (something)", it is obsolete - VB now infers the (something) by itself, so there is no need to state it explicitly.

提交回复
热议问题