title: What is the order of execution for Dispatcher operations(Invoke/BeginInvoke)
body: <p>Here is the test code in WPF:</p>
  <pre><code>            Action ac0 = delegate
              {
                  Console.WriteLine(&quot;action-beginInvoke-enter&quot;);
                  Application.Current.Dispatcher.BeginInvoke(new Action(delegate
                  {
                      Console.WriteLine(&quot;dispatcher begin invoke code&quot;);
                  }));
                  Console.WriteLine(&quot;action-beginInvoke-exit&quot;);
              };
              ac0.BeginInvoke(null, null);
              Console.WriteLine(&quot;ui thread sleep before&quot;);
              Thread.Sleep(1000);// ensure the ac0 is done
              Console.WriteLine(&quot;ui thread sleep after&quot;);
              Application.Current.Dispatcher.Invoke(new Action(delegate
              {
                  Console.WriteLine(&quot;dispatcher invoke code&quot;);
              }));
              Console.WriteLine(&quot;ui thread exit&quot;);
  </code></pre>
  <p>Output:<br />
  ui thread sleep before<br />
  action-beginInvoke-enter<br />
  action-beginInvoke-exit<br />
  ui thread sleep after<br />
  dispatcher invoke code<br />
  ui thread exit<br />
  dispatcher begin invoke code</p>
  <p>Then, I remove the Invoke code into another project and compile as a .dll:</p>
  <pre><code>            Action ac0 = delegate
              {
                  Console.WriteLine(&quot;action-beginInvoke-enter&quot;);
                  Application.Current.Dispatcher.BeginInvoke(new Action(delegate
                  {
                      Console.WriteLine(&quot;dispatcher begin invoke code&quot;);
                  }));
                  Console.WriteLine(&quot;action-beginInvoke-exit&quot;);
              };
              ac0.BeginInvoke(null, null);
              Console.WriteLine(&quot;ui thread sleep before&quot;);
              Thread.Sleep(1000);// ensure the ac0 is done
              Console.WriteLine(&quot;ui thread sleep after&quot;);
              Test.WriteLine();// Test is a static class from dll
              Console.WriteLine(&quot;ui thread exit&quot;);
  </code></pre>
  <p>Here is the Test.WriteLine:</p>
  <pre><code>        Application.Current.Dispatcher.Invoke(new Action(delegate
          {
              Console.WriteLine(&quot;dispatcher invoke code&quot;);
          }));
  </code></pre>
  <p>the output is:<br />
  ui thread sleep before<br />
  action-beginInvoke-enter<br />
  action-beginInvoke-exit<br />
  ui thread sleep after<br />
  dispatcher begin invoke code<br />
  dispatcher invoke code<br />
  ui thread exit</p>
  <p>I'm trying to figure out the order of execution for Dispatcher operations. As I know the UI thread is busy until executing to the last line. How could it execute the code &quot;dispatcher begin invoke code&quot; before that? And the code is the same except the Dispatcher.Invoke is removed into a dll. Why their output is different?</p>