Question Unsure how to use Background Worker Component.

gvjonnyv

Member
Joined
Jan 15, 2009
Messages
7
Programming Experience
Beginner
I've slowly but surely been creating a program to sort though a large amount of user data on our server by folder size and file size (thanks to a lot of help from this forum). I was recently told that using the Background Worker component would help speed this process up... however, after reading through many tutorials, forums and other areas of advice I'm still completely confused on how to incorporate it with my code. Could someone please at least get me started or dumb down the process for me?

VB.NET:
Imports System.IO
Imports System.ComponentModel

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles secondSearch.Click

        'SearchDirectory(TextBox1.Text)
        Dim i As Integer = 0
        Dim x As Integer = ListBox1.Items.Count - 3
        For i = 0 To x Step 2
            MsgBox(x)
            ListBox1.SelectedIndex = i
            SearchDirectory(ListBox1.SelectedItem)
        Next i
        ListBox2.Items.Add("")
        ListBox2.Items.Add("Done.")

    End Sub
    Private Sub SearchDirectory(ByVal sDir As String)
        Dim strDir As String
        Dim strFile As String
        Dim dblFolderSize As Double
        Try
            For Each strDir In System.IO.Directory.GetDirectories(sDir)
                dblFolderSize = GetFolderSize(strDir, True) / 1048576
                If dblFolderSize >= NumericFolder.Value Then
                    ListBox2.Items.Add("Size: " + CStr(Round((dblFolderSize / 100), 2)) + "MB** FOLDER: " + strDir)
                    For Each strFile In System.IO.Directory.GetFiles(strDir)
                        Dim dblFileSize As Double
                        dblFileSize = (GetFileSize(strFile)) / 1048576
                        If dblFileSize >= NumericFile.Value Then
                            ListBox2.Items.Add("          Size: " + CStr(Round(dblFileSize, 2) / 100) + "MB*** FILENAME:   " + strFile)
                            ListBox2.Refresh()
                        End If
                        Application.DoEvents()
                    Next
                End If
                Application.DoEvents()
                SearchDirectory(strDir)
            Next
        Catch ex As System.Exception
            Debug.WriteLine(ex.Message)
        End Try
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles primSearch.Click
        Dim objDir As DirectoryInfo = New DirectoryInfo(TextBox1.Text)
        Dim objFolder As DirectoryInfo
        Dim dblTotalSize As Double
        Dim strToList As String = ""
        Dim strCCList As String = "Done."

        Me.Text = "Please wait..."
        Dim newSub As DirectoryInfo
        newSub = objDir
        For Each objFolder In newSub.GetDirectories()
            dblTotalSize = GetFolderSize(objFolder.FullName, True) / 1048576
            If dblTotalSize >= NumericFolder.Value Then
                strToList = objFolder.FullName
                ListBox1.Items.Add(strToList)
                ListBox1.Refresh()
                ListBox1.Items.Add("   Size:  " + CStr(Round(dblTotalSize, 2) / 100) + "MB")
                ListBox1.Refresh()
                Application.DoEvents()
            End If
            Application.DoEvents()
        Next
        Me.Text = "Folder Size - Done!"
        ListBox1.Items.Add("")
        ListBox1.Items.Add("Done.")
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        FolderBrowserDialog1.ShowDialog()
        TextBox1.Text = FolderBrowserDialog1.SelectedPath
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim streamWriter As StreamWriter = New StreamWriter("I:\results.txt")
        For Each o As Object In ListBox1.Items
            streamWriter.WriteLine(o.ToString())
        Next
        streamWriter.Flush()
        streamWriter.Close()
        MsgBox("Results saved to I:\results.txt")
    End Sub
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        ListBox1.Items.Clear()
        ListBox2.Items.Clear()
    End Sub
    Function Round(ByVal nValue As Double, ByVal nDigits As Integer) As Double
        Round = Int(nValue * (10 ^ nDigits) + (0.5) / (10 ^ nDigits))
    End Function
    Function GetFileSize(ByVal fileSize As String) As Long
        Dim objFileInfo As FileInfo = New FileInfo(fileSize)
        Dim Size As Double
        Size = objFileInfo.Length
        Return Size
    End Function
    Function GetFolderSize(ByVal DirPath As String, Optional ByVal IncludeSubFolders As Boolean = True) As Double
        Dim dblDirSize As Double
        Dim objFileInfo As FileInfo
        Dim objDir As DirectoryInfo = New DirectoryInfo(DirPath)
        Dim objSubFolder As DirectoryInfo
        Try
            For Each objFileInfo In objDir.GetFiles()
                dblDirSize += objFileInfo.Length
            Next
            If IncludeSubFolders Then
                For Each objSubFolder In objDir.GetDirectories()
                    dblDirSize += GetFolderSize(objSubFolder.FullName)
                Next
            End If
        Catch ex As Exception
        End Try
        Return dblDirSize
    End Function
 
Back
Top