当先前的处理程序未完成时,事件是否被触发?

发布于 2024-09-16 13:34:37 字数 289 浏览 7 评论 0原文

当先前的事件处理程序的调用未完成时,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 技术交流群。

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

发布评论

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

评论(2

北凤男飞 2024-09-23 13:34:37

我对 SqlDependency 类不太熟悉,但根据 MSDN 文档:

OnChange 事件可能生成于
与线程不同的线程
启动命令执行。

这似乎开启了两个事件处理程序可以同时运行的可能性。没有文档指出引发事件调用的所有事件处理程序必须完成才能再次引发事件。安全的做法是假设可以同时发生多个事件调用。因为该事件是在未定义的线程上引发的,所以无论如何您都必须防止并发问题。

I am not that familiar with the SqlDependency class, but per the MSDN documenation:

The OnChange event may be generated on
a different thread from the thread
that initiated command execution.

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.

爱给你人给你 2024-09-23 13:34:37

在正常的单线程程序中不会。尝试以下操作:

添加 Windows 窗体应用程序。向表单添加按钮。使用以下方法处理按钮的单击事件:

private void Buttonclick(object sender, EventArgs e)
{
    Thread.Sleep(25);//SUSPEND CURRENT THREAD
}

在调用 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:

private void Buttonclick(object sender, EventArgs e)
{
    Thread.Sleep(25);//SUSPEND CURRENT THREAD
}

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.

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