static EventUnsubscriber() { _threadsCount = OcConfiguration.EventUnsubscriberThreadsCount; _newSubscriptionManualResetEvents = new ManualResetEventSlim[_threadsCount]; __threads = new Thread[_threadsCount]; void threadStart(object state) { int index = (int)state; while (true) { _newSubscriptionManualResetEvents[index].Wait(); _newSubscriptionManualResetEvents[index].Reset(); while (_subscriptionsQueue.TryDequeue(out Subscriptions subscriptions)) { for (int i = 0; i < subscriptions.PropertyChangedEventSubscriptions.Length; i++) { PropertyChangedEventSubscription propertyChangedEventSubscription = subscriptions.PropertyChangedEventSubscriptions[i]; if (propertyChangedEventSubscription.Handler != null) { propertyChangedEventSubscription.Source.PropertyChanged -= propertyChangedEventSubscription.Handler; } } for (int i = 0; i < subscriptions.MethodChangedEventSubscriptions.Length; i++) { MethodChangedEventSubscription methodChangedEventSubscription = subscriptions.MethodChangedEventSubscriptions[i]; if (methodChangedEventSubscription.Handler != null) { methodChangedEventSubscription.Source.MethodChanged -= methodChangedEventSubscription.Handler; } } } } } for (int index = 0; index < _threadsCount; index++) { _newSubscriptionManualResetEvents[index] = new ManualResetEventSlim(false); __threads[index] = new Thread(threadStart) { Name = $"ObservableComputations events unsubscriber #{index}", IsBackground = true }; __threads[index].Start(index); } }
static EventUnsubscriber() { _thread = new Thread(() => { while (true) { _newSubscriptionManualResetEvent.Wait(); _newSubscriptionManualResetEvent.Reset(); while (_subscriptionsQueue.TryDequeue(out Subscriptions subscriptions)) { for (int i = 0; i < subscriptions.PropertyChangedEventSubscriptions.Length; i++) { PropertyChangedEventSubscription propertyChangedEventSubscription = subscriptions.PropertyChangedEventSubscriptions[i]; if (propertyChangedEventSubscription.Handler != null) { propertyChangedEventSubscription.Source.PropertyChanged -= propertyChangedEventSubscription.Handler; } } for (int i = 0; i < subscriptions.MethodChangedEventSubscriptions.Length; i++) { MethodChangedEventSubscription methodChangedEventSubscription = subscriptions.MethodChangedEventSubscriptions[i]; if (methodChangedEventSubscription.Handler != null) { methodChangedEventSubscription.Source.MethodChanged -= methodChangedEventSubscription.Handler; } } } } }); _thread.Name = "ObservableComputations events unsubscriber"; _thread.IsBackground = true; _thread.Start(); }