Results 1 to 4 of 4

Thread: [2005] Application freeze

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    8

    Post [2005] Application freeze

    Hello,

    I am using the following script to browse to a folder and subfolders to search for files with a specific file-extension.
    The problem is, if I have a directory with some subfolders and a large number of files with that extension, everything goes fine.
    When make the script search a entire partition (not my windows partition), the application freezes, but it is still searching (HDD LED flickers).
    I am looking for a solution, because I want the user to be able to see which directory the script is currently searching, and to display a cancel button, which now cannot be pushed while the application will freeze.

    Does anyone know a solution for my problem?

    This is the script I am using.

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim i As Int32
    
            Call searchFiles("E:", "*.extension")
    
            For i = 0 To Files.GetUpperBound(0) - 1
                ListBox1.Items.Add(Files(i))
            Next
        End Sub
    
        Private Files() As String
        Private Counter As Int32
    
        Private Sub searchFiles(ByRef sPath As String, ByRef FileSpec As String)
            Dim Dir As String
            Dim file As String
    
            If sPath.Substring(sPath.Length - 1) <> "\" Then
                sPath &= "\"
            End If
    
            Dim files() As String = Directory.GetFiles(sPath)
    
            For Each file In files
                If file.ToLower Like FileSpec Then
                    ReDim Preserve files(Counter)
                    files(Counter) = file
                    Counter += 1
                End If
            Next
            Dim dirs() As String = Directory.GetDirectories(sPath)
            For Each Dir In dirs
                Call searchFiles(Dir, FileSpec)
            Next
        End Sub
    Thanks.

  2. #2
    Lively Member
    Join Date
    Jul 2007
    Location
    Mechelen
    Posts
    119

    Re: [2005] Application freeze

    Use background worker or a thread. Search it on the forums .

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Application freeze

    Your searchFile sub is VERY inefficient. There are alot you can change to optimize it. Having said that, you can simply use My.Computer.FileSystem.GetFiles method to do what you need
    See example here
    http://msdn2.microsoft.com/en-us/lib...hb(VS.80).aspx

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    8

    Re: [2005] Application freeze

    Stanav, thanks, that is a lot shorter then a custom made function.

    Maxim, I can't get it to work using a BackgroundWorker...
    I have never used a BackgroundWorker before...
    Do you have a example how I can make this script work in the background?

    Code:
        Public Function GetFiles(ByVal directory As String, ByVal searchOption As Microsoft.VisualBasic.FileIO.SearchOption, ByVal wildCards As String) As String
            For Each foundFile As String In My.Computer.FileSystem.GetFiles(directory, searchOption, wildCards)
                ListBox1.Items.Add(foundFile)
            Next
        End Function
    I want the script to update a label when it changes directory while searching, and want to be able to cancel the progress, or display a message when the search is finished.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width