PC Review


Reply
Thread Tools Rate Thread

Baffled by BeginInvoke() callback

 
 
Jon Shemitz
Guest
Posts: n/a
 
      29th Aug 2005
When you use BeginInvoke to call a delegate asynchronously, you can
pass it a AsyncCallback which gets called in the same ThreadPool
thread that runs the 'main' delegate, after the 'main' delegate
returns.

Why? What's the point?

If I want to run This() when That() is done, why wouldn't I just
BeginInvoke a delegate to "void Compound() {That(); This()}" ?

If I want to 'fire and forget' and have the callback call EndInvoke,
why wouldn't I just call ThreadPool.QueueUserWorkItem()?

Have anyone EVER passed anything but null, null as the last two
parameters to BeginInvoke?

<not-totally-stupid>

I did Google this, and I did see lots of sample code that
passes an AsyncCallback - but each and every one was either
a 'fire and forget' or a 'compound' situation.

</not-totally-stupid>

(I suppose it's easier to pass multiple params to an asynch delegate
than to save them to a tiny object that calls the delegate, and that
can then export a parameter-less method suitable for passing to
ThreadPool.QueueUserWorkItem, but you still have to supply a
AsyncCallback delegate to cast the IAsyncResult.AsyncState to the
right delegate type and EndInvoke the IAsyncResult. This doesn't save
much effort, if any, even under 1.1 (and, with anonymous methods, it's
dead simple to write an WaitCallback delegate that does nothing but
pass a bunch of params to a method.) I can't really believe that this
"multiple params" scenario is why every asynch call has this callback
option.)

--

www.midnightbeach.com
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BeginInvoke and Callback Joe Microsoft C# .NET 2 10th May 2006 11:18 PM
Control.BeginInvoke vs. delegate.BeginInvoke Valerie Hough Microsoft C# .NET 6 21st Oct 2005 11:14 AM
BeginInvoke on events with callback problem =?Utf-8?B?U3RhbXBlZGU=?= Microsoft C# .NET 2 7th Sep 2005 09:19 PM
BeginInvoke async callback nick_tucker@hotmail.com Microsoft C# .NET 6 21st Jul 2005 09:02 PM
Baffled ! Neville DIY PC 5 24th Nov 2003 10:45 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:50 AM.