title: How to call Dispatcher.RunAsync() in a thread safe method to update UI components
body: <p>First, I don’t think I have titled this correctly – after much thought.</p>
  
  <p>However, I have been trying to find a way to update a UI control from multiple threads spawned from a Task.Run(async ()=>{ }) expression/method. I’m doing this in an UWP windows mobile 10 application. </p>
  
  <p>I've listed majority of the routines involved, and need help with completing the call to the DisplayProgress(string message) method. In this method, I want to update the UI control in a thread safe manner using Dispathcer.RunAsync(), but need it to be thread safe. Please see <strong>Attempt 1&amp;2</strong> in the code below.</p>
  
  <pre><code>public sealed partial class SynchProcess : BasePage
  {
      protected override void OnNavigatedTo(NavigationEventArgs e)
      {
          Task.Run(async ()=&gt;{
              var result = await SynchTables();
          });
      }
  
      private async Task&lt;bool&gt; SynchTables()
      {
          Bool bRet = true;
          List&lt;Task&gt; tasks = new List&lt;Task&gt;();
          Try
          {
              // Refresh 1
              tasks.Add(Task.Run(async () =&gt; 
              {
                  // Update UI
                  DisplayProgress(“Cars refresh…running”); 
                  List&lt;Car&gt; cars = await _dataService.GetCarsData();
                  DataAccessSQLite.DeleteAll&lt;Car&gt;();
                  DataAccessSQLite.InsertCars(cars);
                  // Update UI
                  DisplayProgress(“Cars refresh…completed”); 
              }));
  
              // Refresh 2
              tasks.Add(Task.Run(async () =&gt;
              {
                  //Update UI
                  DisplayProgress(“Tracks refresh…running”); 
                  List&lt;Track&gt; tracks = await _dataService.GeTrackstData();
                  DataAccessSQLite.DeleteAll&lt;Track&gt;();
                  DataAccessSQLite.InsertTracks(tracks);
                  //Update UI
                  DisplayProgress(“Tracks refresh…completed”); 
  
              }));
  
              // Refresh 3
              // Refresh 4
              …
              …                
  
              Task.WaitAll(tasks.ToArray());
  
          }
          Catch(AggreggateException agEx)
          {
              bRet = false;
              //  removed for brevity
              …
          }
  
          return bRet;
      }
  
      // Attempt  1 : This i was aware of, but included for completeness.
      // FAIL - The await operator can only be used in an async method.
  
      private static void DisplayProgress(string message)
      {
          lock(_lockObject)
          {
              await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =&gt;
              {
                  //Update control logic;
              });
      }
  
      // Attempt 2
      // FAIL - An object reference is required for non-static field, method or   
      //        property ‘DependencyObject.Dispatcher’
  
      private static async Task DisplayProgress (string message)
      {
          await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =&gt;
          {
              // Update UI logic here
          });
      }
  
  
  }
  </code></pre>
  
  <p>I found this article <a href="http://briandunnington.github.io/uitask.html" rel="nofollow noreferrer">http://briandunnington.github.io/uitask.html</a>, however, I’m not sure if this approach is correct or thread safe. Seems like a lot of boilerplate code for something that should be handled by Dispatacher.RunAsync()? Additional articles I’ve read pointed to Action delegates, which I think is supposed to be thread safe, but after several (very detailed technical articles) I am now confused. Additional articles:  <a href="https://stackoverflow.com/questions/6349125/are-c-sharp-delegates-thread-safe">Are C# delegates thread-safe?</a> (unfortunately, I can only link to 2 articles - need to increase my points first !)</p>