title: WPF: Execute some code asynchronously in .NET 3.5
body: <p>I have a MVVM WPF app. I have a window, let's say "LvWindow", with a listview that is loaded from data comming from a database. From main window "MainWindow", I have a menu, which has some options. When I select the option to access "LvWindow", it is open. Then from ViewModel, in the constructor I have a call to a database from which I request some data that then I load into the listview. </p>
  
  <p>My goal is to make the process to request data from database and then load it in the listview asynchronous. I want this in order to not block the whole app, I mean, during this window is loaded, user can go to the main window menu and select to open another type of window. Windows are open in tabs. </p>
  
  <p>While the process of requesting data from database and being loaded into listview in window "LvWindow", I show a splash saying "Loading" on it(in fact this is a rectangle with zindex set to a higher number to avoid user can interact with listview until it is completely loaded). This splash will be closed when the listview is loaded with the data comming from database.</p>
  
  <p>So to make the process asynchronous, I know in winforms it can be done with delegates by using beginInvoke, endInvoke and callbacks methods, see <a href="https://msdn.microsoft.com/es-es/library/2e08f6yc(v=vs.110).aspx" rel="nofollow noreferrer">here</a>.</p>
  
  <p>Also, another possibility is to use a background worker, like posted <a href="https://stackoverflow.com/questions/31414563/how-do-i-execute-an-asynchronous-startup-on-a-wpf-application">here</a>.</p>
  
  <p>So in WPF which is the best way to do it? using delegates as winforms or background workers?</p>
  
  <p><strong>ATTEMPT #1</strong>:
  I have tried XANIMAX solution as this:</p>
  
  <pre><code>public class TestViewModel : BaseViewModel
  {
      private static Dispatcher _dispatcher;
      public ObservableCollection&lt;UserData&gt; lstUsers
  
      public ObservableCollection&lt;UserData&gt; LstUsers
      {
          get
          {
              return this.lstUsers;
          }
  
          private set
          {
              this.lstUsers= value;
              OnPropertyChanged("LstUsers");
          }
      }
  
      public TestViewModel()
      {
          ThreadPool.QueueUserWorkItem(new WaitCallback((o) =&gt;
          {
              var result = getDataFromDatabase();
              UIThread((p) =&gt; LstUsers = result);
          }));
      }
  
      ObservableCollection&lt;UserData&gt; getDataFromDatabase()
      {            
          return this.RequestDataToDatabase();
      }
  
      static void UIThread(Action&lt;object&gt; a)
      {
          if(_dispatcher == null) _dispatcher = Dispatcher.CurrentDispatcher;
          //this is to make sure that the event is raised on the correct Thread
          _dispatcher.Invoke(a); &lt;---- HERE EXCEPTION IS THROWN
      }
  }
  </code></pre>
  
  <p>but in line _dispatcher.Invoke(a) an exception is thrown:</p>
  
  <pre><code>TargetParameterCountException: the parameter count mismatch
  </code></pre>
  
  <p>UserData is my data model, it is a class with some public properties. Something like:</p>
  
  <pre><code>public class UserData
  {
     public string ID{ get; set; }
     public string Name { get; set; }
     public string Surname { get; set; }
  
     // Other properties
  }
  </code></pre>
  
  <p>so the problem is that the call to database is returning "RequestDataToDatabase" is returning a collection of UserData (ObservableCollection) so the exception is thrown. </p>
  
  <p>I do not know how to solve it. Could you help me, please?   </p>
  
  <p><strong>Final solution</strong>:</p>
  
  <p>As XAMIMAX said in the comments:</p>
  
  <ul>
  <li>Change the signature from static void UIThread(Action a) to static void UIThread(Action a)</li>
  <li>modify UIThread((p) => LstUsers = result); by UIThread(() => LstUsers
  = result);</li>
  </ul>