title: How to call a method asynchronously in Silverlight 4?
body: <p>I'm developing a Silverlight app for SharePoint and want to get ListItems from a list. I know I need to query asynchronously to avoid blocking the UI thread. Normally I use ExecuteQueryAsync but that won't work since I like to set the result as DataGrid source. </p>
  
  <p>How can I call the GetItemsFromList method asynchronously and set the result as DataGrid source without creating much code overhead? (lambda?)</p>
  
  <p>SharePointHelper Class:</p>
  
  <pre><code>public static ListItemCollection GetItemsFromList(string name)
  {
      var context = ClientContext.Current;
      var targetList = context.Web.Lists.GetByTitle("ListName");
  
      CamlQuery camlQuery = new CamlQuery();
      camlQuery.ViewXml = string.Format("&lt;View&gt;&lt;Query&gt;&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name='Title'/&gt;&lt;Value Type='Text'&gt;{0}&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;&lt;/Query&gt;RowLimit&gt;4&lt;/RowLimit&gt;&lt;/View&gt;",
                                        name);
  
      ListItemCollection collListItems = targetList.GetItems(camlQuery);
      context.ExecuteQuery();
  
      return collListItems;
  }
  </code></pre>
  
  <p>UI Class:</p>
  
  <pre><code>private void SetDataGridItemSource()
  {
      dataGrid.Source = GetItemsFromList("name");
  }
  </code></pre>
  
  <p>I have now implemented the solution from Shawn Kendrot:</p>
  
  <pre><code>BackgroundWorker worker = new BackgroundWorker();
  worker.DoWork += (sender, args) =&gt; 
  {
      args.Result = SharePointHelpers.GetItemsFromList("name");
  };
  worker.RunWorkerCompleted += (s, e) =&gt; dataSource.Source = e.Result as ListItemCollection;
  worker.RunWorkerAsync(); 
  </code></pre>