- lock (syncRoot) {
- try {
- foreach (var item in callbacks) {
- if (throwOnFirstException) {
- item.Value ();
- } else {
- try {
- item.Value ();
- } catch (Exception e) {
- if (exceptions == null)
- exceptions = new List<Exception> ();
-
- exceptions.Add (e);
- }
+ try {
+ Action cb;
+ for (int id = currId; id != int.MinValue; id--) {
+ if (!callbacks.TryRemove (new CancellationTokenRegistration (id, this), out cb))
+ continue;
+ if (cb == null)
+ continue;
+
+ if (throwOnFirstException) {
+ cb ();
+ } else {
+ try {
+ cb ();
+ } catch (Exception e) {
+ if (exceptions == null)
+ exceptions = new List<Exception> ();
+
+ exceptions.Add (e);