开发者

Affect of Using & RemoveHandler in conjunction with an Async ASMX call

开发者 https://www.devze.com 2022-12-18 20:16 出处:网络
Given the following code Public Shared Sub DoAsyncAction() Using asmxProxy As New MyAsmxServiceProxy() AddHandler as开发者_开发百科mxProxy.WebFunctionCompleted, AddressOf WebFunctionAsync_Completed

Given the following code

    Public Shared Sub DoAsyncAction()
        Using asmxProxy As New MyAsmxServiceProxy()
            AddHandler as开发者_开发百科mxProxy.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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消