I'm building a VB 2005 app (.Net 2.0) that performs searches against our
active directory.
It's pretty basic. I have a UI form that takes search parameters and feeds
them to a class I created. I built a "broker" class that handles the thread
processing which has a reference to the UI and to the Searcher class.
When the user clicks search, it calls a method on the broker to create a
thread that executes the search. The searcher keeps a ref to the broker to
feed results back to it which fires off a delegate to the ref of the client
UI to populate a listview with the results.
On the broker, there is a shared variable called CancelRequested. When the
client clicks the cancel button on the UI it sets the CancelRequested to True
on the broker.
On the Searcher, it checks after each search result to see what the status
of broker.CancelRequested is. If it is true, it exits out of the search loop
and sub and lets the thread terminate normally. Problem is, I can debug the
app and see the searcher evaluate the CancelRequest check and exit the sub, I
can see the thread exit, but the results keep pumping back to the UI.
Any ideas? It may be something to do with the
DirectoryServices.DirectorySearch object I am using, but I can't seem to get
the dang thing to stop. I've waited for several minutes after the cancel
request is processed. I've done a few other apps with this architecture and
they work great.
Here is the "offending" routine:
Private Sub Search()
Dim de As New DirectoryEntry(SearchRoot)
Dim ds As New DirectorySearcher(de)
Dim col As SearchResultCollection = Nothing
ds.Filter = BuildFilter()
Console.WriteLine(ds.Filter)
ds.SearchScope = SearchScope.Subtree
ds.PageSize = 50
Dim sr As SearchResult = Nothing
col = ds.FindAll()
For Each sr In col
If Broker.CancelRequested = True Then
Broker.Completed(True)
Exit Sub
End If
Dim item As New ListViewItem
Dim i As Integer = 0
For i = 0 To Conditions.IncludedAttributes.Length - 1
item.SubItems.Add(CType(sr.Properties(Conditions.I ncludedAttributes(i))(0),
String))
Next
Broker.ResultFound(item)
Next
End Sub