10

I am preforming a long search in active directory and would really like to user the DirectorySearcher.Asynchronous = True. Microsoft provides very little documentation on MSDN

An asynchronous search can show results as they are found while simultaneously searching for additional results. This is useful for tasks such as populating list boxes.

The default setting for this property is false.

How is my application to know when the search is done. I don't see any properties, events or callbacks that would provide this notification. Does anyone have any Idea how to get this functionality?

Basically I am looking for this:

  • Start Async Directory Search
  • Return Results to a System.Collections.Concurrent.ConcurrentQueue(Of Object)
  • As DirectorySearcher is running I can process Items added to the Queue

Thanks so much for your help.

Dakota K
  • 141
  • 1
  • 5
  • Am I going to need to create my own class using System.DirectoryServices.Protocols to get this functionality? – Dakota K Nov 14 '12 at 05:24

2 Answers2

1

DirectoryServices uses ADSI to talk to AD. When you set async to true it sets the ADS_SEARCHPREF_ASYNCHRONOUS search preference to true using IDirectorySearch.SetSearchPreferences.

Here is a page that explains the differences between sync and async searches: Synchronous and Asynchronous Searches with IDirectorySearch

This describes paging: Paging with IDirectorySearch

If you are doing a big query you could spawn your own thread or use the thread pool, set the page size to something below 1000, and populate your queue as the results come in.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
AbdElRaheim
  • 1,384
  • 6
  • 8
0

Appreciate this is a very old question but I've been struggling with this for a couple of days so posting for anyone else. This Can I get more than 1000 records from a DirectorySearcher? answers it well. I read it as the search is still happening as you enumerate the results.

        searcher.PageSize = 100;
        searcher.Asynchronous = true;
        var found = searcher.FindAll();

        foreach (var item in found)
        {
            myList.Add(item);
            Application.Current.Dispatcher.BeginInvoke(new Action(() =>
            {
                ; do something with the results to update your window
            }));
        }

        found.Dispose();
Whoo
  • 1
  • 2