Running a function in a new thread

Hello, this function is making the whole program to freeze when it's working, so I would like to put it in another thread.
The problem is I don't know how to make it work in another thread because of the arguments and to return the value.
Can you help me ?
    Function SearchForFiles(ByVal RootFolder As String, ByVal FileFilter() As String) As List(Of String)
        Dim ReturnedData As New List(Of String)                            
        Dim FolderStack As New Stack(Of String)                             
        FolderStack.Push(RootFolder)                                        
        Do While FolderStack.Count > 0                                      
            Dim ThisFolder As String = FolderStack.Pop                      
            Try                                                             
                For Each SubFolder In GetDirectories(ThisFolder)            
                    FolderStack.Push(SubFolder)                             
                Next                                                        
                For Each FileExt In FileFilter                              
                    ReturnedData.AddRange(GetFiles(ThisFolder, FileExt))    
                Next                                                        
            Catch ex As Exception                                          
            End Try                                                         
        Loop                                                                
        Return ReturnedData                                                 
    End Function

Open in new window

Quentin BédarrideAsked:
Who is Participating?
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.

Dev AdminSystem Engineer | .NET Developer | Microsoft MVP | Technical SpeakerCommented:
Quentin BédarrideAuthor Commented:
The problem for me is to use the multiple arguments required for this function with a backgroundworker, can you give me an example ?
Fernando SotoRetiredCommented:
Hi Quentin Bédarride;

Here is a code sample showing how to do what you need. Please read the comments in the code for more information.
Imports System.IO.Directory
Imports System.ComponentModel

'' Add a BacgroundWork component to the form
'' Select the BacgroundWork component and in the Properties window select Events 
''    And double click on the DoWork and the RunWorkerCompleted events
'' Then try the code below

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        '' Clears the ListBox before executing the BackgroundWorker
        ListBox1.DataSource = Nothing
        '' Runs the BackgroundWorker, the first argument is the folder and the second is the file pattern
        '' Change the directory and file pattern on the next line.
        BackgroundWorker1.RunWorkerAsync(New Object() {"C:\Working Directory", "*.pdf"})
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        '' Gets a reference to the worker object just in case you need it
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        '' Argument 1 the RootFolder and argument 2 string file filter
        Dim RootFolder As String = e.Argument(0)
        Dim FileFilter As String = e.Argument(1)

        Dim ReturnedData As New List(Of String)
        Dim FolderStack As New Stack(Of String)
        FolderStack.Push(RootFolder)

        Do While FolderStack.Count > 0
            Dim ThisFolder As String = FolderStack.Pop
            Try
                For Each SubFolder In GetDirectories(ThisFolder)
                    FolderStack.Push(SubFolder)
                Next
                For Each FileExt In FileFilter
                    ReturnedData.AddRange(GetFiles(ThisFolder, FileExt))
                Next
            Catch ex As Exception
            End Try
        Loop

        '' Return the results to the RunWorkerCompleted subroutine
        e.Result = ReturnedData
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        '' Assign the results to the Windows control
        ListBox1.DataSource = e.Result
    End Sub

End Class

Open in new window

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
Quentin BédarrideAuthor Commented:
Thank you Fernando Soto, I followed your example and it works perfectly !
Fernando SotoRetiredCommented:
Not a problem Quentin Bédarride, glad I was able to help.
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.