Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / November 2003

Tip: Looking for answers? Try searching our database.

reuse method delegate in callback

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Martin Plante - 05 Nov 2003 15:34 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.