J
Jerry West
VB 2005 .NET
I'm trying to enumerate through a folder structure. Everytime I try I
receive an error. The error states:
ContextSwitchDeadlock was detected
The CLR has been unable to transition from COM context 0x1a0de0 to COM
context 0x1a0f50 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.
This occurs at various points in the code --almost never at the same point.
I mean jeez, what the heck? I'm definitely new to VB .NET but this was a
cinch in VB6. It looked like it was going to be even easier in .NET but....
The code:
Dim x As New FileSearch(New
IO.DirectoryInfo(ss.s.ImagePath))
x.Recursive = True
x.Search()
Option Strict On
Option Explicit On
Imports System.IO
Public Class FileSearch
Private Const DefaultFileMask As String = "*.*"
Private Const DefaultDirectoryMask As String = "*"
#Region " Member Variables "
Private _FileMask As String
Private _DirectoryMask As String
Private _Recursive As Boolean
Private _InitialDirectory As DirectoryInfo
Private _Files As New List(Of FileInfo)
Private _Directories As New List(Of DirectoryInfo)
#End Region
#Region " Properites "
Public Property Recursive() As Boolean
Get
Return _Recursive
End Get
Set(ByVal Value As Boolean)
_Recursive = Value
End Set
End Property
Public Property InitialDirectory() As DirectoryInfo
Get
Return _InitialDirectory
End Get
Set(ByVal Value As DirectoryInfo)
_InitialDirectory = Value
End Set
End Property
Public Property DirectoryMask() As String
Get
Return _DirectoryMask
End Get
Set(ByVal Value As String)
_DirectoryMask = Value
End Set
End Property
Public Property FileMask() As String
Get
Return _FileMask
End Get
Set(ByVal Value As String)
_FileMask = Value
End Set
End Property
Public ReadOnly Property Directories() As List(Of DirectoryInfo)
Get
Return _Directories
End Get
End Property
Public ReadOnly Property Files() As List(Of FileInfo)
Get
Return _Files
End Get
End Property
#End Region
#Region " Constructors "
Public Sub New()
End Sub
Public Sub New(ByVal BaseDirectory As String, Optional ByVal FileMask As
String = DefaultFileMask, Optional ByVal DirectoryMask As String =
DefaultDirectoryMask)
Me.New(New IO.DirectoryInfo(BaseDirectory), FileMask, DirectoryMask)
End Sub
Public Sub New(ByVal BaseDirectory As DirectoryInfo, Optional ByVal
FileMask As String = DefaultFileMask, Optional ByVal DirectoryMask As String
= DefaultDirectoryMask)
_InitialDirectory = BaseDirectory
_FileMask = FileMask
_DirectoryMask = DirectoryMask
End Sub
#End Region
Protected Overrides Sub Finalize()
_Files = Nothing
_Directories = Nothing
_Recursive = False
MyBase.Finalize()
End Sub
Public Sub Search(Optional ByVal BaseDirectory As DirectoryInfo =
Nothing, Optional ByVal FileMask As String = Nothing, Optional ByVal
DirectoryMask As String = Nothing, Optional ByVal Recursive As Boolean =
False)
If (Not IsNothing(BaseDirectory)) Then _InitialDirectory =
BaseDirectory
If IsNothing(_InitialDirectory) Then Throw New ArgumentException("A
Directory Must be specified!", "Directory")
If Recursive Then _Recursive = Recursive
If IsNothing(_FileMask) And IsNothing(FileMask) Then
_FileMask = DefaultFileMask
ElseIf (Not IsNothing(FileMask)) Then
_FileMask = FileMask
End If
If IsNothing(_DirectoryMask) And IsNothing(DirectoryMask) Then
_DirectoryMask = DefaultDirectoryMask
ElseIf IsNothing(_DirectoryMask) Then
_DirectoryMask = DirectoryMask
End If
DoSearch(_InitialDirectory)
End Sub
Private Sub DoSearch(ByVal BaseDirectory As DirectoryInfo)
Try
_Files.AddRange(BaseDirectory.GetFiles(_FileMask))
Catch u As UnauthorizedAccessException
'Siliently Ignore this error, there isnt any simple
'way to avoid this error.
End Try
If (Not _Recursive) Then Exit Sub
Try
If _Recursive Then
Dim Directories() As DirectoryInfo =
BaseDirectory.GetDirectories(_DirectoryMask)
_Directories.AddRange(Directories)
Else
Dim d As DirectoryInfo = New DirectoryInfo(ss.s.ImagePath)
_Directories.Add(d)
'Dim Directories() As DirectoryInfo = d
End If
For Each di As DirectoryInfo In Directories
DoSearch(di)
Next
Catch u As UnauthorizedAccessException
'Siliently Ignore this error, there isnt any simple
'way to avoid this error.
End Try
End Sub
End Class
I'm trying to enumerate through a folder structure. Everytime I try I
receive an error. The error states:
ContextSwitchDeadlock was detected
The CLR has been unable to transition from COM context 0x1a0de0 to COM
context 0x1a0f50 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.
This occurs at various points in the code --almost never at the same point.
I mean jeez, what the heck? I'm definitely new to VB .NET but this was a
cinch in VB6. It looked like it was going to be even easier in .NET but....
The code:
Dim x As New FileSearch(New
IO.DirectoryInfo(ss.s.ImagePath))
x.Recursive = True
x.Search()
Option Strict On
Option Explicit On
Imports System.IO
Public Class FileSearch
Private Const DefaultFileMask As String = "*.*"
Private Const DefaultDirectoryMask As String = "*"
#Region " Member Variables "
Private _FileMask As String
Private _DirectoryMask As String
Private _Recursive As Boolean
Private _InitialDirectory As DirectoryInfo
Private _Files As New List(Of FileInfo)
Private _Directories As New List(Of DirectoryInfo)
#End Region
#Region " Properites "
Public Property Recursive() As Boolean
Get
Return _Recursive
End Get
Set(ByVal Value As Boolean)
_Recursive = Value
End Set
End Property
Public Property InitialDirectory() As DirectoryInfo
Get
Return _InitialDirectory
End Get
Set(ByVal Value As DirectoryInfo)
_InitialDirectory = Value
End Set
End Property
Public Property DirectoryMask() As String
Get
Return _DirectoryMask
End Get
Set(ByVal Value As String)
_DirectoryMask = Value
End Set
End Property
Public Property FileMask() As String
Get
Return _FileMask
End Get
Set(ByVal Value As String)
_FileMask = Value
End Set
End Property
Public ReadOnly Property Directories() As List(Of DirectoryInfo)
Get
Return _Directories
End Get
End Property
Public ReadOnly Property Files() As List(Of FileInfo)
Get
Return _Files
End Get
End Property
#End Region
#Region " Constructors "
Public Sub New()
End Sub
Public Sub New(ByVal BaseDirectory As String, Optional ByVal FileMask As
String = DefaultFileMask, Optional ByVal DirectoryMask As String =
DefaultDirectoryMask)
Me.New(New IO.DirectoryInfo(BaseDirectory), FileMask, DirectoryMask)
End Sub
Public Sub New(ByVal BaseDirectory As DirectoryInfo, Optional ByVal
FileMask As String = DefaultFileMask, Optional ByVal DirectoryMask As String
= DefaultDirectoryMask)
_InitialDirectory = BaseDirectory
_FileMask = FileMask
_DirectoryMask = DirectoryMask
End Sub
#End Region
Protected Overrides Sub Finalize()
_Files = Nothing
_Directories = Nothing
_Recursive = False
MyBase.Finalize()
End Sub
Public Sub Search(Optional ByVal BaseDirectory As DirectoryInfo =
Nothing, Optional ByVal FileMask As String = Nothing, Optional ByVal
DirectoryMask As String = Nothing, Optional ByVal Recursive As Boolean =
False)
If (Not IsNothing(BaseDirectory)) Then _InitialDirectory =
BaseDirectory
If IsNothing(_InitialDirectory) Then Throw New ArgumentException("A
Directory Must be specified!", "Directory")
If Recursive Then _Recursive = Recursive
If IsNothing(_FileMask) And IsNothing(FileMask) Then
_FileMask = DefaultFileMask
ElseIf (Not IsNothing(FileMask)) Then
_FileMask = FileMask
End If
If IsNothing(_DirectoryMask) And IsNothing(DirectoryMask) Then
_DirectoryMask = DefaultDirectoryMask
ElseIf IsNothing(_DirectoryMask) Then
_DirectoryMask = DirectoryMask
End If
DoSearch(_InitialDirectory)
End Sub
Private Sub DoSearch(ByVal BaseDirectory As DirectoryInfo)
Try
_Files.AddRange(BaseDirectory.GetFiles(_FileMask))
Catch u As UnauthorizedAccessException
'Siliently Ignore this error, there isnt any simple
'way to avoid this error.
End Try
If (Not _Recursive) Then Exit Sub
Try
If _Recursive Then
Dim Directories() As DirectoryInfo =
BaseDirectory.GetDirectories(_DirectoryMask)
_Directories.AddRange(Directories)
Else
Dim d As DirectoryInfo = New DirectoryInfo(ss.s.ImagePath)
_Directories.Add(d)
'Dim Directories() As DirectoryInfo = d
End If
For Each di As DirectoryInfo In Directories
DoSearch(di)
Next
Catch u As UnauthorizedAccessException
'Siliently Ignore this error, there isnt any simple
'way to avoid this error.
End Try
End Sub
End Class