
Return value in multi-threaded app?
Not sure if it's the right answer, but one way of doing this is having the
object expose the results as a property. Then, use events to handle each
time a searcher announces it has completed.
void DoSearch( ){
Search searcher = new Searcher( "c:\myfiles\" );
searcher.OnSearchCompleteEvent += new EventHandler(
HandleOnSearchCompleteEvent );
Thread t = new Thread( new ThreadStart( searcher.Search );
Quote:
}
void HandleOnSearchCompleteEvent( object sender, EventArgs e ){
Searcher searcher = (Searcher) sender;
ResultsCollection col = ( searcher.SearchResults );
Quote:
}
From this code, you could deduce that the Searcher class might look
something like...
public class Searcher{
public event EventHandler OnSearchCompleteEvent;
public Searcher( string location ){ ... }
public void Search(){ ... }; //raises event when complete
public ResultsCollection SearchResults{ get{ ... } }
Quote:
}
Hope this helps. It would be interesting to see how others approach this
too!
Tobin Harris
Quote:
> Hello, I am just beginning to work with threads and threading and am using
> multiple threads to search multiple locations, a fairly straightforward
> piece. However, I am stuck at one point involving collecting the results
of
> each thread's execution. For example I have several objects, each with a
> method such as public static ResultCollection Search(); Before starting
> the thread, I set the object's Query property to the query then pass in
the
> Search method to the ThreadStart delegate.
> However, how can I retrieve the returned value for each thread? Every
> example I have seen uses Console.Write() from within the method so that
> doesnt really return anything back to my own code.
> I hope this issue is clear, sorry for any confusion but I'm mainly
wondering
> how to collect a series of returned values from a series of threads
called.
> Thank you again and enjoy your day!
> Sincerely,
> Christopher