使用及影响RemoveHandler 与异步 ASMX 调用结合使用

发布于 2024-08-19 20:00:07 字数 1025 浏览 2 评论 0原文

鉴于以下代码,

    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

我想知道如何在此处维护事件处理程序。因此,假设 WebFunctionAsync() 在内部需要大约 30 秒才能完成。当时间到了时,它将触发 WebFunctionCompleted 事件,但即使 webClient 已被释放并超出范围,我的处理程序 (WebFunctionAsync_Completed) 是否仍然会被命中?

如果最后一个答案的问题是“是”,如果我在“RemoveHandler”行中进行注释会怎样?那么会吗?

我想我想找出的是,在调用异步函数时,注册的事件处理程序是否与调用一起“缓存”(可以这么说),这样无论 ASMX 代理对象发生什么情况或者即使处理程序被删除,调用时注册的事件处理程序在事件触发时仍然会被命中?

也许这真的很明显,但由于某种原因,我似乎无法从逻辑上得出结论,而且我在 MSDN 上查看的几个地方都没有找到任何答案。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴情 2024-08-26 20:00:07

恕我直言,这不仅仅是删除处理程序的问题。在这种情况下,您不应使用 Using 块。代理将作为 Completed 事件处理程序的 sender 参数传递给您,并且在到达那里时不应将其释放。

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.

┾廆蒐ゝ 2024-08-26 20:00:07

我应该从一开始就这样做,但我整理了一个小测试用例,这是我的结果。

...但是我的处理程序会吗
(WebFunctionAsync_Completed) 仍然得到
即使 webClient 已经命中
已被处置并超出范围?

使用上面提供的代码,将调用处理程序,但正如 @JohnSaunders 指出的,事件处理程序的 sender 参数将是 dispose< /em> MyAsmxServiceProxy 对象的实例。如果您不打算在处理程序中使用它,我想可能没什么大不了的,但仍然值得注意。

如果最后一个答案的问题是
是的,如果我在
删除处理程序行。那么会吗?

RemoveHandler 行被注释时,会出现竞争条件。如果在触发 Completed 事件之前调用 RemoveHandler 行,则永远不会调用该处理程序。所以第二个问题的答案是

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文