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.

Reply via email to