VLC_WINRT.ViewModels.MainPage.MusicLibraryViewModel.StartIndexing C# (CSharp) Method

StartIndexing() private method

private StartIndexing ( ) : System.Threading.Tasks.Task
return System.Threading.Tasks.Task
        private async Task StartIndexing()
        {
            // TODO: Rewrite function.
            _artistDataRepository = new ArtistDataRepository();
            var musicFolder = await
                KnownVLCLocation.MusicLibrary.GetFoldersAsync(CommonFolderQuery.GroupByArtist);
            TimeSpan period = TimeSpan.FromSeconds(10);

            _periodicTimer = ThreadPoolTimer.CreatePeriodicTimer(async (source) =>
            {
                if (Locator.MusicLibraryVM.Track.Count > _numberOfTracks)
                {
                    await DispatchHelper.InvokeAsync(() => Locator.MusicLibraryVM._numberOfTracks = Track.Count);
                }
                else
                {
                    _periodicTimer.Cancel();
                    await DispatchHelper.InvokeAsync(() =>
                    {
                        IsLoaded = true;
                        IsBusy = false;
                    });
                }
            }, period);

            using (await _artistLock.LockAsync())
                foreach (var artistItem in musicFolder)
                {
                    IsMusicLibraryEmpty = false;
                    MusicProperties artistProperties = null;
                    try
                    {
                        artistProperties = await artistItem.Properties.GetMusicPropertiesAsync();
                    }
                    catch
                    {
                        Debug.WriteLine("Could not get artist item properties.");
                    }

                    // If we could not get the artist information, skip it and continue.
                    if (artistProperties == null || artistProperties.Artist == string.Empty)
                    {
                        continue;
                    }

                    StorageFolderQueryResult albumQuery =
                        artistItem.CreateFolderQuery(CommonFolderQuery.GroupByAlbum);

                    // Check if artist is in the database. If so, use it.
                    ArtistItem artist = await _artistDataRepository.LoadViaArtistName(artistProperties.Artist);
                    if (artist == null)
                    {
                        artist = new ArtistItem { Name = artistProperties.Artist };
                        Artist.Add(artist);
                        await _artistDataRepository.Add(artist);
                    }
                    await artist.Initialize(albumQuery, artist);
                    OnPropertyChanged("Track");
                    OnPropertyChanged("Artist");
                }
            OnPropertyChanged("Artist");
        }