Simple program using a Background Worker freezes anyway

Devon Nullman 20 Reputation points
2024-05-28T18:23:19.2266667+00:00

Program gets every filename from a folder, then creates a text list of unique extensions found and the frequency. In this case there are around 35000 files. I put most of the work into the Background Worker but the UI freezes anyway until it is finished. Code is:

Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.Text
Imports System.IO
Public Class Form1
    Private Counter As Integer = 0
    Private SB As New StringBuilder
    Private Filenames As ReadOnlyCollection(Of String)
    Private Frequencies As New SortedDictionary(Of String, Integer)
    Private Extension As String = ""
    Private Filename As String = ""
    Private Sub BtnStart_Click(sender As Object, e As EventArgs) Handles BtnStart.Click
        TxtProgress.Clear()
        TxtResults.Clear()
        SB.Clear()
        Frequencies.Clear()
        Filenames = My.Computer.FileSystem.GetFiles("G:\Steve-DELLSERVER\SharedProgs&Data", FileIO.SearchOption.SearchAllSubDirectories, "*.*")
        BGW1.RunWorkerAsync()
    End Sub
    Private Sub BGW1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BGW1.DoWork
        For Counter = 0 To Filenames.Count - 1
            If Counter Mod 10000 = 0 Then
                BGW1.ReportProgress(Counter)
            End If
            Filename = Filenames(Counter)
            Extension = Path.GetExtension(Filename).ToUpper
            If Frequencies.ContainsKey(Extension) Then
                Frequencies(Extension) += 1
            Else
                Frequencies.Add(Extension, 1)
            End If
        Next
    End Sub
    Private Sub BGW1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BGW1.ProgressChanged
        TxtProgress.Text = e.ProgressPercentage.ToString
    End Sub
    Private Sub BGW1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BGW1.RunWorkerCompleted
        TxtProgress.Text = "File # " & Filenames.Count.ToString.ToString & " of " & Filenames.Count.ToString
        For Each KVP As KeyValuePair(Of String, Integer) In Frequencies
            SB.AppendLine(KVP.Key & vbTab & KVP.Value.ToString)
        Next
        TxtResults.Text = SB.ToString
    End Sub
End Class

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,641 questions
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 28,001 Reputation points Microsoft Vendor
    2024-05-29T01:34:33.86+00:00

    Hi @Devon Nullman ,

    You are retrieving the list of files from the directory is done on the UI thread. When you call My.Computer.FileSystem.GetFiles, it blocks the UI thread until it completes, which can take a significant amount of time for a large number of files.

    Just move file retrieval to Background Worker.

    
        Private Sub BGW1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BGW1.DoWork
    
            Dim 
    
            For Counter = 0 To 
            ...
        End Sub
    
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. RLWA32 42,196 Reputation points
    2024-05-29T00:52:42.9166667+00:00
            Filenames = My.Computer.FileSystem.GetFiles("G:\Steve-DELLSERVER\SharedProgs&Data", FileIO.SearchOption.SearchAllSubDirectories, "*.*")
    
    

    This line of code is executed on the UI thread. That causes the application to be unresponsive.

    0 comments No comments

  2. Devon Nullman 20 Reputation points
    2024-05-29T06:05:50.3266667+00:00

    What got me is that the actual processing (adding to the Dictionary) took 300 milliseconds so the progress steps weren't shown. Making the list (GetFiles) took 4 seconds

    Thanks

    0 comments No comments