GetDirectories problem/error

nbrege

Active member
Joined
Jan 12, 2006
Messages
34
I get the following error:

The CLR has been unable to transition from COM context 0x1a01d0 to COM context 0x1a0340 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.

when I run the following code:

[VB]
Dim files As ReadOnlyCollection(Of String)

files = My.Computer.FileSystem.GetDirectories(pathString, FileIO.SearchOption.SearchAllSubDirectories)
[/VB]

This just gets all the sub-directories in a specified folder. The error only occurs when pathString is set to a folder with a lot of subfolders in it (4000+).

I dont know how to interpret the error message to make this code work. I just know that after 60 seconds the program errors out. Can anyone tell me what I need to do to make this work when there is a lot of folders/subfolders? Thanks...
 
I dont know how this will work with 4000+ directories, but this works for me. Personally I stay away from the My namespace, regardless this is what I use and it should work the same way.

Fire up a new console app and try this out on the dir that gives you trouble and let me know what happens...

Code:
Imports System.IO

Module Module1
    Sub Main()
        Dim st As String()
        Change this drive to the directory that errors for you and post back the results.
        st = Directory.GetDirectories("d:\\")

        For Each str As String In st
            Console.WriteLine(str)
        Next

        Console.Read()
    End Sub
End Module
 
I got the impression from the original post that the code is supposed to search through each subfolder, not simply list the top level subfolders (I cant be sure as Ive never used the My namespace). If this is the case you will need to call the method recursively. If this is the case there have been alot of posts about this recently so just search the forum.
 
I ofcourse havent tested this on 4000 subfolders but it seems to work pretty quick.

First though, the other code posted, I beleive will return the entire path along with the subfolders

You might wrather try this

Code:
Imports System.IO

Public Class Form1


    Sub FolderRefresh()
        list1.Items.Clear()
        Dim dirArr() As String = System.IO.Directory.GetDirectories("C:\Program Files\Your Folder Here\")
        For Each itm As String In dirArr
            list1.Items.Add(System.IO.Path.GetFileName(itm))

        Next
    End Sub



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Call FolderRefresh()

    End Sub
End Class

This will return all Subfolders within a Directory... just the FolderNames not the full path too.

Hope this helps

vbMarkO
 
Back
Top