Baffling problem with DirectoryInfo.GetFiles

D

Dominic Marks

Hello Group,

I'm bemused as to why this code isn't working. It's a
very simple loop over the array of FileInfo objects
returned from DirectoryInfo.GetFiles. It works fine until
the eigth iteration at which point it blows up, everytime.

Sample Code:
...
Imports System.IO
...
Dim fInfo, fsInfo() As FileInfo
Dim dirInfo = New DirectoryInfo ("c:\")
fsInfo = dirInfo.GetFiles
For Each fInfo in fsInfo
Console.WriteLine fInfo.Name
Next
...

It collapses saying 'The handle is invalid'. Looking at
things in the debugger only leads to more confusion since
I can see 14, apparently valid, FileInfo objects in fsInfo.

Changing to a different directory, in fact, changing anything
makes no difference. It's always the eigth time.

What the heck?

Dominic
 
D

Dominic Marks

Dominic said:
Hello Group,

Changing to a different directory, in fact, changing anything
makes no difference. It's always the eigth time.

What the heck?

Dominic

I seem to be making a habit of replying to my own questions,
anyway - I discovered it was, as usual, all my own fault :)

When using a ThreadPool beware of the main thread exiting in
the background, I assumed my that since all the programs data
was in the thread, it wouldn't matter. For example my Main
consists of just:

Public Shared Sub Main()
ThreadPool.QueueUserWorkItem(AddressOf TraverseFileSystem, _
New String("C:\"))
Thread.Sleep(1000)
End Sub

But extending the Sleep to 100 seconds fixes the problem, so
it clearly matters. If anyone knows why it matters, I assume
its a fairly self-explainatory reason I'd be interested to
find out.

Thanks,
Dominic
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top