views:

70

answers:

2

Given the following code

    Public Shared Sub DoAsyncAction()
        Using asmxProxy As New MyAsmxServiceProxy()
            AddHandler asmxProxy.WebFunctionCompleted, AddressOf WebFunctionAsync_Completed

            // Make the Async ASMX Webservice Call
            asmxProxy.WebFunctionAsync()

            // RemoveHandler asmxProxy.WebFunctionCompleted, AddressOf WebFunctionAsync_Completed
        End Using
    End Sub

    Private Shared Sub WebFunctionAsync_Completed(ByVal sender As Object, ByVal e As MyAsmxServiceProxy.WebFunctionCompletedEventArgs)
        // Do Stuff
    End Sub

I was wondering how the event handler is maintained here. So, let say the WebFunctionAsync() internally takes ~30 seconds to complete. When that time is up, it will fire the WebFunctionCompleted event, but will my handler (WebFunctionAsync_Completed) still get hit even thought the webClient has been disposed and gone out of scope?

If the question to the last answer is Yes, what if I commented in the RemoveHandler line. Would it then?

I guess what I'm trying to find out is, at the time the async function is called, are the registered event handlers "cached" (so to speak) along with the call, so that no matter what happens to the ASMX proxy object or even if the handlers are removed, the registered event handlers at the time the call will still be hit when the events fire?

Maybe this is really obvious, but for some reason I can't seem to logically come to a conclusion on this, and I didn't find any answers in the few places I looked on MSDN.

+1  A: 

IMHO, it's not just a question of the Removehandler. You shouldn't use a Using block in this case. The proxy is being passed to you as the sender parameter of the Completed event handler, and should not be disposed when it gets there.

John Saunders
Interesting thought. I actually had no intended desire to reference sender in the handler, so I never considered that fact. I guess reading between the lines here, you are saying that even if the asmxProxy is disposed, the event handler will still be called in this case -- answering the first question I had. Right?
ckittel
Should it then be the responsibility of the SendHtmlEMailCompleted event handler to .Dispose() of sender (and maybe even first RemoveHandler)?
ckittel
@ckittel: yes. Even if you don't explicitly use the proxy instance in the handler, consider that it will likely be used just before the handler is called - to dispatch to the handler!
John Saunders
A: 

I should have done this from the beginning, but I put together a little test case and here were my results.

... but will my handler (WebFunctionAsync_Completed) still get hit even thought the webClient has been disposed and gone out of scope?

With the code as presented above, Yes the handler will be called, but as @JohnSaunders pointed out, the sender parameter of the event handler will be the disposed instance of the MyAsmxServiceProxy object. Probably not a big deal if you don't plan on using it in the handler I suppose, but still worth noting.

If the question to the last answer is Yes, what if I commented in the RemoveHandler line. Would it then?

With the RemoveHandler line commented in, a race condition occurs. If the RemoveHandler line is called before the Completed event is fired, then the handler is never invoked. So the answer to the second question is No.

ckittel