是C#的活动同步? [英] are C# events synchronous?

查看:151
本文介绍了是C#的活动同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个部分对这个问题:

  1. 确实提高的事件阻塞线程,或者它开始执行事件处理的异步线程的推移继续在同一时间?

  2. 个别事件处理器的(订阅事件)同步运行一个接一个,或者是他们无法保证其他人不会在同一时间运行的异步运行?

解决方案

这是一个笼统的回答,反映了默认行为:

  1. 是的,它会阻止线程,如果订阅该事件的方法是不同步的。
  2. 它们是可以一前一后。这有另一种扭曲:如果一个事件处理程序抛出一个异常,尚未执行的事件处理程序不会被执行

说了这么多,每一个类,它提供事件,可以选择异步执行的事件。 ■设计提供一类被称为 EventsHelper ,简化了这一点。

[注] 此链接要求您提供一个电子邮件地址下载EventsHelper类。

There are two parts to this question:

  1. Does raising an event block the thread, or does it start execution of EventHandlers asynchronously and the thread goes continues on at the same time?

  2. Are the individual EventHandlers (subscribed to the event) run synchronously one after another, or are they run asynchronously with no guarantee that others aren't running at the same time?

解决方案

This is a general answer and reflects the default behavior:

  1. Yes, it blocks the thread, if the methods subscribing to the event are not asynchronous.
  2. They are executed one after the other. This has another twist: If one event handler throws an exception, the event handlers not yet executed will not be executed.

Having said that, every class that provides events can choose to implement its event asynchronously. IDesign provides a class called EventsHelper that simplifies this.

[Note] this link requires you to provide an e-mail address to download EventsHelper class.

这篇关于是C#的活动同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆