当您调用事件时,事件是阻塞的还是非阻塞的?
假设我有一个名为 Tick
的事件:
public class Test
{
public event Action Tick;
public void Test()
{
Tick();
Console.WriteLine("Tick Finished");
}
}
如果我订阅了很多事件 Tick
,则运行 Test() 的线程的操作将如何
被阻塞,直到它们全部被调用,还是异步执行?
Let's say I have an event Tick
which I call:
public class Test
{
public event Action Tick;
public void Test()
{
Tick();
Console.WriteLine("Tick Finished");
}
}
If I have loads of events subscribed to Tick
, will the operation of this thread that is running Test()
be blocked until they have all been called or does it perform this asynchronously?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所有事件都被阻止。线程执行将被阻塞,直到注册到该事件的所有事件处理程序都被执行为止。
All events are blocking. The thread execution will be blocked until all the event handlers registered to this event have been executed.
当 Tick() 运行时,它将阻止您进一步执行代码。
为了向自己证明这一点,请编写一个控制台应用程序来执行此操作,该应用程序拥有大量事件订阅者,然后观察会发生什么。
While Tick() runs, it will block you from progressing further through the code.
To prove it to yourself, write a console app to do just that, that has a bunch of subscribers to the event, and watch to see what happens.