使用及影响RemoveHandler 与异步 ASMX 调用结合使用
鉴于以下代码,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
恕我直言,这不仅仅是删除处理程序的问题。在这种情况下,您不应使用
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 thesender
parameter of theCompleted
event handler, and should not be disposed when it gets there.我应该从一开始就这样做,但我整理了一个小测试用例,这是我的结果。
使用上面提供的代码,是将调用处理程序,但正如 @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.
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 theMyAsmxServiceProxy
object. Probably not a big deal if you don't plan on using it in the handler I suppose, but still worth noting.With the
RemoveHandler
line commented in, a race condition occurs. If theRemoveHandler
line is called before theCompleted
event is fired, then the handler is never invoked. So the answer to the second question is No.