Threading Question/Issue

D

David Miller

Dear Sir or Madam:

I am writing an application in VB.Net, and I have an interesting threading situation that I cannot seem to figure out. I have a search class (CSearch) that is responsible for performing asynchronous searches. It has methods as follows...

'Starts a new asyncronous search, and displays the results in a new window.
'The results window can also be suppressed if no results are found
Public Sub StartSearchAsync(ByVal query As CQuery, ByVal openHandler As ObjectOpenedHandler, ByVal suppressEmptyResults As Boolean, ByVal existingSearchID As Integer, ByVal title As String, ByVal startIconIdx As Integer, ByVal endIconIdx As Integer)
'Create a new search delegate
Dim searchDeleg As SearchHandler = New SearchHandler(AddressOf StartSearch)

'Create a new search identifier
Dim ident As CSearchIdentifier
ident = New CSearchIdentifier
ident.AsyncDelegate = searchDeleg
ident.ObjectOpenMethod = openHandler
ident.SuppressEmptyResults = suppressEmptyResults
ident.StartedIconIndex = startIconIdx
ident.FinishedIconIndex = endIconIdx
[Code Omitted]

Dim ar As IAsyncResult
ar = searchDeleg.BeginInvoke(query, New AsyncCallback(AddressOf SearchCompleted), ident) <-- The query object here has two items in its collection.
End Sub



'This method is called whenever a search is completed
Private Sub SearchCompleted(ByVal ar As IAsyncResult)
'Get the search identification object
Dim ident As CSearchIdentifier
ident = DirectCast(ar.AsyncState, CSearchIdentifier)
'Get the delegate back
Dim deleg As SearchHandler
deleg = DirectCast(ident.AsyncDelegate, SearchHandler)
'Get the results from the delegate
Dim results As CollectionBase = deleg.EndInvoke(ar)
'Raise the event
RaiseEvent SearchFinished(ident, results)
End Sub



'Starts a new search and blocks until the operation completes
Public Function StartSearch(ByVal query As CQuery) As CollectionBase
'Perform the search
Dim results As CollectionBase = Nothing
results = CFacade.ExecuteQuery(query) <--- Now the query object has 0 items in its collection.

'Return the results
Return results
End Function

The StartSearchAsync method is called from another class in order to perform the search. The CQuery object has a collection in it exposed through a property that has several search conditions objects in it. My issue is that on the marked line in StartSearchAsync, the query object has two objects in its collection, but when the StartSearch method is invoked through the delegate, the query object somehow loses its two objects in its collection. This is shown in bold on the second marked line.

Does anyone have any ideas about why this might by occurring? If you need any more info on the code, a better explanation, or anything else, just let me know.

Thanks,

David Miller
 
N

Nice Chap

Have you tried changing the Signature to

StartSearchAsync(ByRef query As CQuery ??
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top