当先前的处理程序未完成时,事件是否被触发?
当先前的事件处理程序的调用未完成时,SqlDependency(例如)是否会触发 OnChange 事件? (假设OnDependencyChange方法非常耗时) 到底发生了什么?
SqlDependency dependency=new SqlDependency(command);
// Subscribe to the SqlDependency event.
dependency.OnChange += new
OnChangeEventHandler(OnDependencyChange);
Does SqlDependency (for example) fires the OnChange event, when the invocation of previous event handler didn't finish? (Assume that OnDependencyChange method is very time consuming)
What exactly happens?
SqlDependency dependency=new SqlDependency(command);
// Subscribe to the SqlDependency event.
dependency.OnChange += new
OnChangeEventHandler(OnDependencyChange);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对 SqlDependency 类不太熟悉,但根据 MSDN 文档:
这似乎开启了两个事件处理程序可以同时运行的可能性。没有文档指出引发事件调用的所有事件处理程序必须完成才能再次引发事件。安全的做法是假设可以同时发生多个事件调用。因为该事件是在未定义的线程上引发的,所以无论如何您都必须防止并发问题。
I am not that familiar with the
SqlDependency
class, but per the MSDN documenation:That seems to open up the possibility that two event handlers can run simultaneously. There is no documentation that states that all event handlers invoked from raising the event must complete before the event can be raised again. The safe thing to do is to assume multiple simultaneous invocations of the event can occur. Because the event is raised on an undefined thread you will have to guard against the problems of concurrency anyway.
在正常的单线程程序中不会。尝试以下操作:
添加 Windows 窗体应用程序。向表单添加按钮。使用以下方法处理按钮的单击事件:
在调用
Thread.Sleep(25)
完成其工作(挂起线程 25 秒)之前,您将无法单击该按钮。您也可以在控制台应用程序中对此进行测试。In normal single threaded program not it will not. Try following:
Add windows forms app. Add a button to a form. Use following method to handle button's click event:
you will not be able to click the button until the call to
Thread.Sleep(25)
finishes its work (suspend thread for 25 secs). You can test this in Console app also.