title: c# UWP scan for music files
body: <p>I am implementing a music player for windows 10, and i am in a bit of a pickle at the library phase.</p>
  
  <p>I am scanning for all the files in music library (plus optional folders the user selects) and i need to also get their tags (only need the title and artist, don't need complex details).</p>
  
  <p>The problem is performance. Getting the music properties cost ALOT if you have a large media library. At the moment i am doing the following:</p>
  
  <pre><code>private void UserControl_Loaded(object sender, RoutedEventArgs e)
      {
          Task.Factory.StartNew(async () =&gt;
          {
              try
              {
                  var files = await StorageHelper.GetFiles(KnownFolders.MusicLibrary);
                  List&lt;LocalAudio&gt; tempList = new List&lt;LocalAudio&gt;();
                  foreach (var file in files)
                  {
                      MusicProperties properties = await file.Properties.GetMusicPropertiesAsync();
  
                      var title = string.IsNullOrWhiteSpace(properties.Title) ? file.Name : properties.Title;
                      var artist = string.IsNullOrWhiteSpace(properties.Artist) ? file.Path : properties.Artist;
                      tempList.Add(new LocalAudio() { Title = title, FilePath = file.Path, Artist = artist, Duration = properties.Duration });
                      if (tempList.Count &gt; 50)
                      {
                          await AddToLibrary(tempList);
                          tempList.Clear();
                      }
                  }
                  await AddToLibrary(tempList);
                  tempList.Clear();
              }
              catch (Exception ex)
              {
                  //log exception here
              }
          });
      }
  
      private async Task AddToLibrary(List&lt;LocalAudio&gt; tempList)
      {
          await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =&gt;
          {
              foreach (var item in tempList)
              {
                  this.LocalFiles.Add(item);
              }
          });
      }
  </code></pre>
  
  <p>Where "LocalAudio" is my audio model and this.LocalFiles is an ObservableCollection.
  Also, the StorageHelper.GetFiles(StorageFolder) method returns an IEnumerable of StorageFiles (music files) (it's a recursive method to go in depth).</p>
  
  <p>I am adding batches of 50 files on the dispatcher thread so i won't block the UI (i know, even if it is an async method running in a task that i started by fire-and-forget it still blocks the UI if i add them continuously...).</p>
  
  <p>My question is: how can i optimize this better?
  Any idea is good. I was thinking to scan them once and save the music properties (title, artist and duration) in a file and read that file on application start and just sync the filename with the data i have in that file (a dictionary or something and read the file and keep it in memory). Not sure how good this idea is.</p>
  
  <p>Thank you for taking your time reading my silly questions :)</p>