title: Dispatcher.BeginInvoke not being executed in one case
body: <p>I'm trying to fix a bug in an existing application, where in a single case, a dispatched event is not being executed.</p>
  
  <p>In our application, there are several services implementing a common abstract class (AbstractService). The issue I've got occurs when a service saves an entity and has to raise a notification in the UI thread.</p>
  
  <p>Relevant part of AbstractService:</p>
  
  <pre><code>public abstract class AbstractService&lt;TEntity, TData, TInfo&gt; : IDataProvider, IAbstractService
      where TEntity : AbstractEntity, new()
      where TData : AbstractData&lt;TEntity&gt;
      where TInfo : IEntityInfo {
  
      [...]
  
      protected virtual void NotifyEntityChanged(NotifyEventItem&lt;TInfo&gt;[] pNotifyEventItems) { }
  
      private void NotifyPersPlanChanged() {
          if (PersPlanChanged != null)
              PersPlanChanged(this, new NotifyEventArgs&lt;IEntityInfo&gt;(null, PersistanceState.Reset));
  
          Debug.WriteLine(string.Format("{0} {1} Call {2} to NotifyPersPlanChanged",
              DateTime.Now.ToLongTimeString(), GetType().Name, m_counterNotifyPersPlanChanged++));
      }
  
      private static int m_counterInternalFireNotification;
      private void InternalFireNotification(List&lt;NotifyEventItem&lt;TInfo&gt;&gt; pNotifyEventItem) {
          if (pNotifyEventItem == null || pNotifyEventItem.Count == 0)
              return;
  
          Debug.WriteLine(string.Format("{0} {1} Call {2} to InternalFireNotification",
              DateTime.Now.ToLongTimeString(), GetType().Name, m_counterInternalFireNotification++));
  
          // do not call the event handlers directly, because a transaction may be in progress and
          // we want the event handlers to see the end results of the modifications.
          Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new HandleEntityChanged(NotifyEntityChanged), pNotifyEventItem.ToArray());
          Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new HandlePersPlanChanged(NotifyPersPlanChanged));
      }
  }
  </code></pre>
  
  <p>Every time a property of an entity has been modified through the service, the <code>NotifyEntityChanged</code> is being called.</p>
  
  <p>Here are two implementations of <code>AbstractService</code> out of the ~20. The first one, is the one I'm having issues with, the second works. I'll describe further down what the issue is.</p>
  
  <pre><code>public class ErgaenzungsfeldService : AbstractServiceBasic&lt;ErgaenzungsfeldEntity, ErgaenzungsfeldData&gt;, IErgaenzungsfeldService {
  
      [...]
  
      [EventPublication(Constants.TopicErgaenzungsfeldChanged, PublicationScope.Global)]
      public event EventHandler ErgaenzungsfeldChanged;
  
      private static int m_counterNotifyEntityChanged;
      protected override void NotifyEntityChanged(NotifyEventItem&lt;IEntityInfoBasic&gt;[] pNotifyEventItems) {
          Debug.WriteLine(string.Format("{0} {1} Call {2} to NotifyEntityChanged",
              DateTime.Now.ToLongTimeString(), GetType().Name, m_counterNotifyEntityChanged++));
  
          base.NotifyEntityChanged(pNotifyEventItems);
          if (ErgaenzungsfeldChanged != null) {
              ErgaenzungsfeldChanged(this, new NotifyEventArgs&lt;IEntityInfoBasic&gt;(pNotifyEventItems));
          }
      }
  }
  
  public class DienstleistenderService : AbstractAdaLanguageDependentServiceVersion&lt;DienstleistenderEntity, DienstleistenderData&gt;, IDienstleistenderService {
  
      [...]
  
      [EventPublication(Constants.TopicDienstleistenderChanged, PublicationScope.Global)]
      public event EventHandler DienstleistenderChanged;
  
      private static int m_counterNotifyEntityChanged;
      protected override void NotifyEntityChanged(NotifyEventItem&lt;IEntityInfoVersion&gt;[] pNotifyEventItem) {
          Debug.WriteLine(string.Format("{0} {1} Call {2} to NotifyEntityChanged",
              DateTime.Now.ToLongTimeString(), GetType().Name, m_counterNotifyEntityChanged++));
          if (DienstleistenderChanged != null) {
              DienstleistenderChanged(this, new NotifyEventArgs&lt;IEntityInfoVersion&gt;(pNotifyEventItem));
          }
      } 
   }
  </code></pre>
  
  <p>All services are instantiated like this:</p>
  
  <pre><code>WorkItem.RootWorkItem.Services.AddNew&lt;ErgaenzungsfeldService, IErgaenzungsfeldService&gt;();
  WorkItem.RootWorkItem.Services.AddNew&lt;DienstleistenderService, IDienstleistenderService&gt;();
  </code></pre>
  
  <p>And accessed using either of these two ways:</p>
  
  <pre><code>WorkItem.Services.Get&lt;IDienstleistenderService&gt;()
  
  [ServiceDependency]
  public IErgaenzungsfeldService ErgaenzungsfeldService { get; set; }
  </code></pre>
  
  <p><strong>Issue:</strong> ErgaenzungsfeldService.NotifyEntityChanged is not always being called. This only happens when importing data and NOT when using the application 'normally'. By normal I mean an ErgaenzungsfeldEntity can be modified in the application and saved. The same mechanism is being used and works.</p>
  
  <p>Some observations:</p>
  
  <ul>
  <li>The import process is single-threaded. It does not run in the UI thread.</li>
  <li>DienstleistenderService.NotifyEntityChanged is being called correctly, when ErgaenzungsfeldService.NotifyEntityChanged is not</li>
  <li>Replacing <code>Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new HandleEntityChanged(NotifyEntityChanged), pNotifyEventItem.ToArray())</code> by <code>NotifyEntityChanged(pNotifyEventItem.ToArray())</code> or by <code>System.Windows.Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new HandlePersPlanChanged(NotifyPersPlanChanged));</code> calls ErgaenzungsfeldService.NotifyEntityChanged correctly. As the UI gets notified from another thread, the application obviously throws exceptions later on.</li>
  <li>If an ErgaenzungsfeldEntity is being saved through the UI, ErgaenzungsfeldService.NotifyEntityChanged is being called correctly.</li>
  </ul>
  
  <p>Here's the output of the messages which have been added for debugging purposes to the different methods which helped me analyze the issue.</p>
  
  <pre><code>09:39:44 ErgaenzungsfeldService Call 0 to InternalFireNotification
  09:39:44 ErgaenzungsfeldService Call 1 to InternalFireNotification
  09:39:44 ErgaenzungsfeldService Call 2 to InternalFireNotification
  09:39:44 ErgaenzungsfeldService Call 3 to InternalFireNotification
  09:40:08 DienstleistenderService Call 0 to InternalFireNotification
  09:40:08 DienstleistenderService Call 1 to InternalFireNotification
  09:40:09 DienstleistenderService Call 2 to InternalFireNotification
  [...]
  09:40:14 DienstleistenderService Call 652 to InternalFireNotification
  09:40:14 DienstleistenderService Call 653 to InternalFireNotification
  09:40:14 DienstleistenderService Call 654 to InternalFireNotification
  09:40:14 DienstleistenderService Call 655 to InternalFireNotification
  09:40:14 AdaService Call 0 to InternalFireNotification
  09:40:14 DienstleistenderService Call 656 to InternalFireNotification
  09:40:14 DienstleistungService Call 0 to InternalFireNotification
  09:40:14 AufbauorganisationService Call 0 to InternalFireNotification
  09:40:14 AufbauorganisationService Call 1 to InternalFireNotification
  09:40:14 AdaService Call 0 to NotifyPersPlanChanged
  09:40:14 DienstleistenderService Call 0 to NotifyEntityChanged
  09:40:16 DienstleistenderService Call 0 to NotifyPersPlanChanged
  09:40:16 DienstleistungService Call 0 to NotifyPersPlanChanged
  09:40:16 AufbauorganisationService Call 0 to NotifyPersPlanChanged
  09:40:16 AufbauorganisationService Call 1 to NotifyPersPlanChanged
  </code></pre>
  
  <p><strike><strong>Question:</strong> What could cause the notification not to be executed as described?</strike></p>
  
  <p><strong>[Edit]</strong> I've gained some more insight by storing the <code>DispatcherOperation</code>:</p>
  
  <pre><code>private List&lt;DispatcherOperation&gt; m_dispatcherOperationresults = new List&lt;DispatcherOperation&gt;();
  private void InternalFireNotification(List&lt;NotifyEventItem&lt;TInfo&gt;&gt; pNotifyEventItem) {
     if (pNotifyEventItem == null || pNotifyEventItem.Count == 0)
        return;
  
     m_log.DebugFormat("InternalFireNotification called for {0}. TInfo type is {1}", GetType(), typeof(TInfo));
  
     // do not call the event handlers directly, because a transaction may be in progress and
     // we want the event handlers to see the end results of the modifications.
     DispatcherOperation result = Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Send, new HandleEntityChanged(NotifyEntityChanged), pNotifyEventItem.ToArray());
     m_dispatcherOperationresults.Add(result);
     Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new HandlePersPlanChanged(NotifyPersPlanChanged));
  }
  </code></pre>
  
  <p>This made clear that the delegate executions are stuck in the execution queue. No matter if DispatcherPriority is set to ApplicationIdle (as it always was) or Send (which is the highest priority), they stay stuck for ErgaenzungsfeldService.</p>
  
  <p>In this printscreen, items 0-3 have been triggered from a non-UI thread during import and stay pending, while item 4 has been triggered from the GUI thread (minutes later) and executed
  <img src="https://i.stack.imgur.com/2HeEt.png" alt="dispatcher priority results"></p>
  
  <p><strong>Question</strong>: What could be the reason for those delegates to remain pending for certain classes, while they work for other classes?</p>