title: Threading.task and Dispatcher.Invoke method in the Window Close event
body: <p>I want to perform series of operations synchronously while closing my MVVM based WPF Application.</p>
  
  <p>Right now I am using Task and <code>Dispatcher.Invoke</code> within the tasks to show the message to the user.</p>
  
  <p>The issue is that when i used <code>Dispatcher.Invoke</code> method in the <code>myfunction</code> function, application gets stuck there. I know this function is working properly when I used these function other than closed event.</p>
  
  <p>So is there any issue of using the <code>Dispatcher.Invoke</code> method in the Close event os the application. How can i solve this?</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Main window closing
  /// &lt;/summary&gt;
  private void MainWindowClosing(object args)
  {
       var task1 = System.Threading.Tasks.Task.Factory.StartNew(() =&gt;
       {
           myfunction();
       }).ContinueWith((cc) =&gt; {  });
       task1.Wait();
  }
  
  private void myfunction()
  {
      //my serries of operation will come here.
      System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =&gt;
      {
          MessageBox.Show("test");
      }));   
  }
  </code></pre>