Calling EndInvoke is option, just as paying attention to the return value of a method is optional. If the function you call returns something (or a reference to something), you're free to ignore it (and let the garbage collector do it's thing later if the method is returning a reference to an object). Same holds true for delegates. You only need to call EndInvoke if you're interested in harvesting the outcome of the method that was called (i.e., it's return value, any out/ref args, or any exception that might be thrown). If you don't care about the outcome, don't call EndInvoke.
This is true whether or not the method you're calling has a void/non-void return value. -Mike http://staff.develop.com/woodring http://www.develop.com/devresources > -----Original Message----- > From: Moderated discussion of advanced .NET topics. > [mailto:[EMAIL PROTECTED]] On Behalf Of Ed Stegman > Sent: Monday, January 20, 2003 8:06 PM > To: [EMAIL PROTECTED] > Subject: [ADVANCED-DOTNET] Bad mojo to not call Delegate.EndInvoke() ? > > > What are the ramifications of not calling EndInvoke() on a > delegate used to asynchronously invoke a method with a void return? > > I'm curious if this variation of the typical Begin/End Invoke > pattern will have any bad side effects. > (Please ignore any obvious typos. Outlook is a forgiving IDE) > > //Some method I wish to invoke async. > private void Notify(IPerson person, Warning warning) { > Transport transport = GetPreferredTransport(person); > switch (transport) { > case (Transport.Email): > SendMail(person.Email, person.Name, warning); > break; > //case ... > } > person.Warnings.Add(warning, transport); > } > > //Delegate used to invoke Notify() asynchronously. > private delegate void NotifyDelegate (IPerson person, Warning > warning); > > //Method used to perform async invoke of Notify() method. > void BeginNotify(IPerson person, Warning warning) { > NotifyDelegate nd = new NotifyDelegate(this.Notify); > AsyncCallback callback = new AsyncCallBack(this.EndNotify); > > //Here's the beginning of the pattern change. I'm passing > //the reference from the person argument as the argument to > //Notify() as usual, and I'm also passing the reference as > //the argument to the delegate's State parameter. > > nd.BeginInvoke(person, warning, callback, person); > } > > //Callback method > void EndNotify(IAsyncResult ar) { > //This is my questionable change to the standard pattern. > //Skip casting ar to an AsyncResult. > //Skip getting reference to the NotifyDelegate instance > // that was created in BeginNotify(). (AsyncResult.AsyncDelegate) > //Skip calling AsyncDelegate.EndInvoke(); > > //Simply cast ar.AsyncState back to our IPerson. > IPerson person = (IPerson)ar.AsyncState; > > //Use person. > ... > } > > Related question: What about not calling EndInvoke() on a > delegate with non void return signature when you don't care about the > return value? > > > Thanks, and > Keep Smilin' > Ed Stegman > > You can read messages from the Advanced DOTNET archive, > unsubscribe from Advanced DOTNET, or > subscribe to other DevelopMentor lists at http://discuss.develop.com. > You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced DOTNET, or subscribe to other DevelopMentor lists at http://discuss.develop.com.
