Running a function in a new thread

Quentin Bédarride
Quentin Bédarride used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
DevAdminSystem Engineer | .NET Developer | Microsoft MVP | Technical Speaker

Commented:

Author

Commented:
The problem for me is to use the multiple arguments required for this function with a backgroundworker, can you give me an example ?
Retired
Distinguished Expert 2017
Commented:
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

Author

Commented:
Thank you Fernando Soto, I followed your example and it works perfectly !
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Not a problem Quentin Bédarride, glad I was able to help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial