Returning Data from a Thread

M

Mike

Hello,

I was wondering what's the best way to return data from a
worker-thread back to the main "calling" thread?

1) Specifying a callback delegate that the thread calls upon
completion.

2) Rasiing an event upon thread compeltion.


Which is the best method? What are the pros/cons? I cannot really seem
to figure out why one would be better than the other.

Are there other more efficient options?

Thanks
-Mike
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (Mike) scripsit:
I was wondering what's the best way to return data from a
worker-thread back to the main "calling" thread?

1) Specifying a callback delegate that the thread calls upon
completion.

2) Rasiing an event upon thread compeltion.


Which is the best method? What are the pros/cons? I cannot really seem
to figure out why one would be better than the other.

<http://www.devx.com/dotnet/Article/11358>

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>
 
M

Matthew Reynolds

Hi Mike,

Neither one is standing out to me as being wholly better than the other.

However, it's worth considering that if you do implement an event,
*technically* it should be implemented as an event pattern, which means that
your event must match this signature:

public event DataLoaded(object sender, DataLoadedEventArgs e)

....therefore you need to return your data in a class that inherits from
EventArgs.

....plus a protected override must exist that actually raised that event,
e.g.

protected void OnDataLoaded(DataLoadedEventArgs e)
{
if(DataLoaded != null)
DataLoaded(this, e)
}

If you don't want to use this pattern (and I suspect you may not), then a
delegate would be just fine - although it's worth bearing in mind that
delegates are a little more fiddly for a caller to use.

Cheers,
Matthew
http://www.dotnet247.com/
http://weblogs.asp.net/mreynolds/
 

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