ArcSet
ArcSet

Reputation: 6860

VB.NET DirectorySearcher Paging

I have been looking for away to figure out how many pages have been searched so i can multi thread the search

Example Lets say Active Directory has 5000 Computer. Active Directory Will only return 1000 Computers a query. The following code will return 5 pages of 1000 results. How would i figure out How many pages DirectorySearcher will have to do in order to get all the results?

Thank you

Dim Searcher As DirectorySearcher = New DirectorySearcher("(objectClass=computer)")
Searcher.PageSize = Integer.MaxValue
Searcher.SizeLimit = Integer.MaxValue
Dim Result As SearchResultCollection = Searcher.FindAll()
For Each i As SearchResult In Result
//some code
Next

Upvotes: 1

Views: 2018

Answers (1)

Sean Hall
Sean Hall

Reputation: 7878

I don't recommend trying to multithread an LDAP query. The individual searches are independent. Let's say you have two threads where the first asks for the first 2500 and second gets the rest. Notice what happens if the first thread makes it query, and one of those computers gets deleted before the second thread makes its query. The 2501th computer was just outside the first thread's range, and then just outside the second thread's range. You won't find this computer in your query.

I'm going to assume that you want to multithread because the FindAll method takes too long. By setting the PageSize to Integer.MaxValue, you're forcing the DC to process the whole query before sending you the results. If you want FindAll to return faster, set a smaller page size. As long as you set the page size, DirectorySearcher abstracts away the fact that it has to ask the server for more results (otherwise it only returns the first 1000 results). Another thing to look at is that prior to Server 2008, the objectClass attribute wasn't indexed.

Finally, if you really do want to distribute the pages among multiple threads, use the System.DirectoryServices.Protocols namespace. It's at a lower level than System.DirectoryServices, so you can do things like asynchronous searches and ask for the pages yourself.

Upvotes: 2

Related Questions