i have (want) to execute a search request to multiple sources.
Now i’ve done some multithreading in the past, but it was all fire and forget.
Now what i want to do, is to spin up 3 identical requests on 3 different objects, wait until they are all ‘done’ (and that gives me the first question: how do they say ‘i’m done’, and then collect all the data thet’ve sent me.
So in pseudo code i have this interface:
interface ISearch
SearchResult SearchForContent(SearchCriteria criteria)
So in code i create the three search services:
ISearch s1 = new SearchLocal();
ISearch s2 = new SearchThere();
ISearch s3 = new SearchHere();
And then call SearchForContent(SearchCriteria criteria) on all three of them, in a multihreaded / async way
and the they all come back to me with their SearchResult and after they are ALL done, i process their SearchResult objects.
I hope these lines of text kindof makes you get what is in my head 🙂
i’m working on a ASP.Net 3.5 C# project.
Create
AutoResetEventand pass them toWaitHandle.WaitAll()There is an example here.
Basically:
1) You create an
AutoResetEventfor each search and passfalseto its constructor.2) Create the threads and run search for each one and at the end, call
Seton theAutoResetEventin the finally block. It is very important that callingSetis done inside the finally block otherwiseWaitAll()will be waiting indefinitely.3) In the code right after you have spawned the threads, you call
WaitHandle.WaitAll()and pass all thoseAutoResetEventto it. This code will wait until all is finished.