• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 170
  • Last Modified:

Searching files and folders

Hello everyone..
I have this problem in making a simple search like the windows search to search files and folders.
I want it to search within the specified folder and subfolders inside it...and return all the files matching the query.

Here is what Ive tried.,.

code:--------------------------------------------------------------------------------
Dim s As String = TextBox1.Text
        For Each file As String In My.Computer.FileSystem.GetFiles("c:\", FileIO.SearchOption.SearchAllSubDirectories, s)
            ListView1.Items.Add(file)
        Next file
--------------------------------------------------------------------------------
.but the problem is...My program freezes till the search ends unlike the windows search.

Someone help...Thank you so much in advance :-)
0
godwindotnet
Asked:
godwindotnet
  • 2
  • 2
1 Solution
 
doobdaveCommented:
Hi there,

The problem you are experiencing is, I believe, related to threading (do a bit of reading up on this topic if you're not familier with the concepts).

Basically what is happening is that your form/UI and the 'search' function are all running in the same thread, which means that everything is executed in a sequential manner, and therefore each function must complete before the code moves on to the next one.

What you need to do is execute the Search function in a separate thread, which means it will go off and do its work, but your UI will remain responsive as it's running in its own thread.

The following code should help you, put it inside the button click event handler, or wherever it is you call the Search function:

Dim thr as System.Threading.Thread
thr = New system.Threading.Thread(AddressOf NameOfYourSearchSub)
thr.Start

Note that your search procedure should be a sub annd not a function and should not take in any parameters, or things become more complicated.

Hope this helps.

Best Regards,

David
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
It's not ~that~ simple...

You should NOT be updating the man UI interface from within your thread.  So this line may cause problems:

    ListView1.Items.Add(file)

You need to use a Delegate to marshal the call from the Thread back onto the main UI Thread.  I can post an example if you like.

Alternatively you can ~cheat~ by adding a call to Application.DoEvents() inside your loop:

        Dim s As String = TextBox1.Text
        For Each file As String In My.Computer.FileSystem.GetFiles("c:\", FileIO.SearchOption.SearchAllSubDirectories, s)
            ListView1.Items.Add(file)
            Application.DoEvents()
        Next file

Just make sure that you don't allow the user to start another search while the code is runing.  This can be achieved by disabling buttons etc., until the operation is complete.
0
 
doobdaveCommented:
Good point Idle_Mind, I should've noticed that!
0
 
godwindotnetAuthor Commented:
Idle_Mind,That wasnt the perfect solution though
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Hi godwindotnet,

Then please give feedback so we know that the proposed solutions either didn't work or still don't perform as well as you want.

Without comments from you we will just assume that they solved the problem.

I'm almost always willing to give examples when they are asked for!

=)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now