Hello.
I'm using a method delegate and calling BeginInvoke with a callback. I'm
passing the delegate and callback in a data holder class as the state
object. In the callback, I'm processing my stuff and wish to call
BeginInvoke again. Is it safe to reuse the same instance of my method
delegate (and callback delegate) in this call?
// Initial call somewhere
MethodDelegate md = new MethodDelegate( this.MyMethod );
AsyncCallback ac = new AsyncCallback( this.MyCallback );
StateData data = new StateData( md, ac );
md.BeginInvoke( ac, data );
private void MyCallback( IAsyncResult result )
{
StateData data = ( StateData )result.AsyncState;
data.MethodDelegate.EndInvoke( result );
// ... Do stuff
data.MethodDelegate.BeginInvoke( data.Callback, data );
}
The question is not "will that work?", as my tests already suggest it does.
I only want to make sure I'm not falling in any pitfalls here.
Thanks.

Signature
Martin Plante
Xceed Software Inc.
http://www.xceedsoft.com
Shri Borde [MSFT] - 14 Nov 2003 18:59 GMT
It should work fine.
If you have a standard signature, you may prefer to use
ThreadPool.QueueUserWorkItem instead of an asynchronous delegate. Unless
you are calling through remoting, QueueUserWorkItem is likely to be faster.
(It avoids any remoting overhead).
With a high rate of async operations, such a model (tail re-posting) can be
problematic since it can lead to stacked callbacks and stack overflow. But
its unlikely to be a real problem
Shri Borde [MSFT]
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
> From: "Martin Plante" <plantem.removethis@xceedsoft.com>
> Subject: reuse method delegate in callback
[quoted text clipped - 40 lines]
>
> Thanks.