Return value in multi-threaded app? 
Author Message
 Return value in multi-threaded app?

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



Sat, 30 Apr 2005 17:00:05 GMT  
 Return value in multi-threaded app?

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!

Well, you could have a shared list of some description, which each
thread adds its own results to. Alternatively, you could have one object
of some class (the one with the delegate in) per thread, and that could
keep the results so that the result of a particular thread's work could
be queried.

--

http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too



Sat, 30 Apr 2005 17:28:29 GMT  
 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




Sat, 30 Apr 2005 17:38:13 GMT  
 Return value in multi-threaded app?
Thanks for Christopher!
I think this code will help me also! ;-)

eau


Quote:
> 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 );
> }

> void HandleOnSearchCompleteEvent( object sender, EventArgs e ){
>     Searcher searcher = (Searcher) sender;
>     ResultsCollection col = ( searcher.SearchResults );
> }

> 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{ ... } }
> }

> Hope this helps. It would be interesting to see how others approach this
> too!

> Tobin Harris


> > 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




Sat, 30 Apr 2005 21:56:27 GMT  
 Return value in multi-threaded app?
Hi Christopher,

I prefer to use a simple approach, a helper class that handles the
thread function and its result

public class ThreadResults
{
        public int iResult = 0;

        public void Threadloop()
        {
           while (iResult < 500)
                iResult++;
        }

Quote:
}

private void button1_Click(object sender, System.EventArgs e)
{
    // create helper class
   ThreadResults tResults = new ThreadResults();

   // start the trhead like any thread
   Thread t = new Thread(new ThreadStart(tResults.Threadloop));
   t.Start();

   // for my example purpose only - wait until thread finished
   t.Join();

   MessageBox.Show("int value = " + tResults.iResult.ToString());

Quote:
}

Charles Steinhardt[MVP]
(To Email: remove _SPAM_ME_NOT_ from return address)


Sun, 01 May 2005 00:11:31 GMT  
 Return value in multi-threaded app?
Hi Charles,

This is probably me being dumb, but if you use thread.Join to wait for the
thread to complete, what is the point of using a thread, since you're
program is stuck waiting for something to complete in a synchronous fashion?
Also, how would this work with multiple threads? I'm new to threading, so
sorry if I've missed the point!

Regards,

Tobin Harris


Quote:
> Hi Christopher,

> I prefer to use a simple approach, a helper class that handles the
> thread function and its result

> public class ThreadResults
> {
> public int iResult = 0;

> public void Threadloop()
> {
>    while (iResult < 500)
> iResult++;
> }
> }

> private void button1_Click(object sender, System.EventArgs e)
> {
>     // create helper class
>    ThreadResults tResults = new ThreadResults();

>    // start the trhead like any thread
>    Thread t = new Thread(new ThreadStart(tResults.Threadloop));
>    t.Start();

>    // for my example purpose only - wait until thread finished
>    t.Join();

>    MessageBox.Show("int value = " + tResults.iResult.ToString());
> }

> Charles Steinhardt[MVP]
> (To Email: remove _SPAM_ME_NOT_ from return address)



Sun, 01 May 2005 21:42:15 GMT  
 Return value in multi-threaded app?
Hi Tobin,

Please read my comment above the original line in the post:

  // for my example purpose only - wait until thread finished
   t.Join();

I was just proving a point showing it works having nothing to do with
running a required thread.  Did the other return value logic make
sense, though?

Charles Steinhardt[MVP]
(To Email: remove _SPAM_ME_NOT_ from return address)



Thu, 05 May 2005 10:12:30 GMT  
 Return value in multi-threaded app?
Yeah, sorry, that makes sense. How do you normally react to when threads
complete, do you trap events on them?

Tobin Harris


Quote:
> Hi Tobin,

> Please read my comment above the original line in the post:

>   // for my example purpose only - wait until thread finished
>    t.Join();

> I was just proving a point showing it works having nothing to do with
> running a required thread.  Did the other return value logic make
> sense, though?

> Charles Steinhardt[MVP]
> (To Email: remove _SPAM_ME_NOT_ from return address)



Fri, 06 May 2005 19:59:15 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. MFC UI Thread doesnt start when COM method called in multi-threaded MFC dialog App

2. Debugger for Multi-Threaded app.

3. How to build a Multi-Threaded App

4. DAO and Multi-threaded apps.

5. Visual Studio 6 locks up when debugging multi-thread app

6. How to build a Multi-Threaded App

7. Single file write with multi-threaded app

8. How to build a Multi-Threaded App

9. well designed multi-threading app

10. distributed multi-threaded App...

11. wait loop in multi-threaded app

12. well designed multi threading app

 

 
Powered by phpBB® Forum Software