Recursively Search for files of a Type using BackgroundWorker and Threading


I have a Backgroundworker that searches for all files on a drive with a specific pattern.  It may take some time running in a Backgroundworker so I can remain responsive in the UI.  What I want to do is to see if I can use threads to make the process a bit faster.

Would I just use one thread or multiple?  I have included the following code to show you what I have so far...

Code from Button_DatabaseSearch
        'Setup for BackgroundWorker
        myArguments.sDrive = sDrive
        myArguments.sFilter = sFilter
        myArguments.dsDataSet = clsDataGridViewDataSource.dsDataGridViewDataSource
        bgw_DBSearchWorker.WorkerReportsProgress = True
        bgw_DBSearchWorker.WorkerSupportsCancellation = True

Open in new window

Code from the DoWork in the BackgroundWorker
        Dim myArgs As bgwArguments = CType(e.Argument, bgwArguments)
        Dim sDrive As String = myArgs.sDrive
        Dim sFilter As String = myArgs.sFilter
        Dim odsDataset As DataSet = myArgs.dsDataSet
        Dim oclsUtilities As New clsDataGridViewDataSource_ConnectionMethod
        Dim oclsDataSource As New clsDataGridViewDataSource
        oclsUtilities.subLocateDataSourceFiles(sDrive, sFilter, odsDataset)

Open in new window

And the code that actually performs the search
    Public Sub subLocateDataSourceFiles _
            ByVal sDrive As String,
            ByVal sFilter As String,
            ByVal dsDataSourceLink As DataSet

        Dim o_dInfo As DirectoryInfo
        Dim o_sInfo As DirectoryInfo
        Dim o_fInfo As FileInfo
        Select Case sDrive
            Case "Initialize"
                Call subDataTable_Initialize(dsDataSourceLink)
            Case Else
                    o_dInfo = New DirectoryInfo(sDrive)
                    For Each o_fInfo In o_dInfo.GetFiles(sFilter)
                        Call subDataTable_RowAdd(o_fInfo, dsDataSourceLink)
                    Next o_fInfo
                    For Each o_sInfo In o_dInfo.GetDirectories
                        subLocateDataSourceFiles(o_sInfo.FullName, "*.MDF", dsDataSourceLink)
                    Next o_sInfo
                Catch ex As Exception
                End Try
        End Select

    End Sub

Open in new window

So with this shown I want to see if I could add Threading to make the search a bit faster (obviously dependent on hardware as well).

Would I use one thread or two (one for Directory and one for files in directory).
Peter AllenAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russ SuterSenior Software DeveloperCommented:
Using multiple threads actually doesn't make things faster. In fact, it makes things slightly slower in most cases. Where it is particularly useful is in freeing up the calling thread to process additional requests. For example, if you have a process that takes a long time you could run that in a worker thread which prevents the user interface from freezing giving the application the appearance that it has locked up. In your case you should do the searching in a single worker thread. It may be possible to split things up into multiple threads and have the system search multiple subdirectories simultaneously but the potential issues you could run into involving thread locking, race conditions, and reporting back to the calling thread make me want to run away from that idea.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽Commented:
Have you considered using the new Async/Await functionality that the Framework offers? It's intended use is for awaiting I/O operations while not causing your UI thread to block. Channel 9 has a good series of videos on the topic.

If I recall correctly, you need to be in .NET 4.5 (maybe 4) in order to use them.
Peter AllenAuthor Commented:
Well it is probably best then - don't want to add more issues.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.