|
-
Mar 14th, 2008, 05:51 PM
#1
Thread Starter
New Member
[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.
-
Mar 14th, 2008, 05:53 PM
#2
Lively Member
Re: [2005] Application freeze
Use background worker or a thread. Search it on the forums .
-
Mar 14th, 2008, 06:13 PM
#3
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
-
Mar 14th, 2008, 08:15 PM
#4
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|