|
-
Sep 21st, 2007, 09:55 AM
#1
Thread Starter
Lively Member
Thread
Is there anyway to return a value from a thread?
thanks,
-zd
-
Sep 21st, 2007, 01:51 PM
#2
I wonder how many charact
Re: Thread
I always point people to this, and its an excellent resource to understand why you want to use a thread to begin with, and highlights various scenarios and solutions:
http://www.albahari.com/threading/threading.pdf
-
Sep 21st, 2007, 02:12 PM
#3
Thread Starter
Lively Member
Re: Thread
Nemaroller,
this is probably one of the best examples on threading i have ever seen.
thanks,
-zd
-
Sep 21st, 2007, 07:45 PM
#4
Re: Thread
In .NET 2.0 the solution is simple in many cases. If you use a BackgroundWorker to implement your background thread then getting data into and out of the thread is dead easy.
To start the ball rolling you call the BGW's RunWorkerAsync method and optionally pass a single object as a parameter. This will raise the DoWork event on a background thread, thus your DoWork event handler is executed on that background thread. If you passed in a parameter then you get this back from the e.Argument property. The object can be anything you like, so it can contain as much data as you like.
At the end of the DoWork event handler you can optionally assign an object to the e.Result property. Once the method completes the RunWorkerCompleted event is raised on the UI thread, thus your RunWorkerCompleted event handler is executed on the UI thread. If you set e.Result in the DoWork event handler then you get that data back from the e.Result property in the RunWorkerCompleted event handler. Just like the data you passed in, this single object can be anything you like, so it can contain as much data as you like.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|