Link to home
Start Free TrialLog in
Avatar of wally_davis
wally_davisFlag for United States of America

asked on

VB.NET - Retrieving Computers from Active Directory crashes app

I am using VB.NET to retrieve Computers from Active Directory and pumps all of the computer names into a text file.
It then crashes the app and produces the following error:

"ContextSwitchDeadlock was detected - The CLR has been unable to transition from COM context 0x1a0928 to COM context 0x1a0a98 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."
Not sure what all this means, but, if there is a more efficient way to perform this routine so it doesn't crash my vb.net module, I would be one estatic person.

My thanks to anyone who can help,
Wallace
Public Function runAdQuery() As DataTable
        Dim entry As New DirectoryEntry("LDAP://DC=MyDomain,DC=Com")
        Dim searchOU As New DirectorySearcher(entry)
        searchOU.Filter = ("(objectClass=Computer)")
        searchOU.PageSize = 100000
 
        Dim adTable As New DataTable("ADTable")
        Dim pcName As DataColumn = adTable.Columns.Add("Workstation_Name", GetType(String))
        pcName.AllowDBNull = True
        pcName.Unique = True
 
        Dim resEnt As SearchResult
        Dim adComputers As StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(adPcList, False)
        For Each resEnt In searchOU.FindAll()
            Try
                Dim de As DirectoryEntry = resEnt.GetDirectoryEntry
                Dim adRow As DataRow = adTable.NewRow
                adRow("Workstation_Name") = de.Properties("Name").Value.ToString
                adTable.Rows.Add(adRow)
                adComputers.WriteLine(resEnt.GetDirectoryEntry.Name) '(adRow.ItemArray(0))
                adComputers.Flush()
            Catch ex As Exception
 
            End Try
        Next
 
        Return runAdQuery

Open in new window

Avatar of Sancler
Sancler

I'm far from sure that it would produce the error you're reporting but one problem with your code is that it never closes adComputers

Roger
Avatar of wally_davis

ASKER

Roger, the adComputers.Flush method causes the value to be written to file/flushed from the buffer and then moves onto retrieving the next workstation value until all values have been read.
However, in Task Manager, I noticed while testing my code that the memory consumption went from 127 mb up to 901 mb before crashing. I have no idea why it consumed so much memory from such small value reads from Active Directory. I also noticed that the Commit Charge Total ran over the Commit Charge Limit and that's when the app produced the error a second time and then crashed.
Not sure what else to do but re-write the routine using other methods altogether.
ASKER CERTIFIED SOLUTION
Avatar of Sancler
Sancler

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you gentleman. You've given me insight and workarounds to help fix the code.