How to prevent UI from freezing during lengthy process?

Posted by OverTheRainbow on Stack Overflow See other posts from Stack Overflow or by OverTheRainbow
Published on 2010-03-15T12:45:42Z Indexed on 2010/03/15 14:19 UTC
Read the original article Hit count: 220

Filed under:
|

Hello,

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:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'TODO Find way to avoid freezing UI while scanning fixed drives

    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
                'Do stuff
            Next filepath
        End If
    Next drive
End Sub

Google returned information on a BackGroundWorker control: Is this the right/way to solve this issue? If not, what solution would you recommend, possibly with a really simple example?

FWIW, I read that Application.DoEvents() is a left-over from VBClassic and should be avoided.

Thank you.

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about ui