/// <summary> /// Download the files for all given ISearchResults to the locations specified by the given ITargetDirectoryProvider. /// based on their type (sound/music) /// </summary> /// <param name="results"></param> private void DeployRequiredFilesForSearchResults(IEnumerable <ISearchResult> results, ITargetDirectoryProvider targetDirectoryProvider) { // Collect the list of files to be deployed List <IDeployableAudioFile> filesToBeDeployed = new List <IDeployableAudioFile>(); foreach (ISearchResult searchResult in results) { // Queue the ISearchResult itself for deployment if it's an IDeployableAudioFile in it's own right if (searchResult is IDeployableAudioFile) { filesToBeDeployed.Add(searchResult as IDeployableAudioFile); } // Queue additional required files for deployment foreach (IDeployableAudioFile deployableFile in searchResult.RequiredFiles) { filesToBeDeployed.Add(deployableFile); } } // Collect the total "cost" of all files to be deployed double totalDeploymentCost = 0; foreach (IDeployableAudioFile deployableFile in filesToBeDeployed) { totalDeploymentCost += deployableFile.DeploymentCost.GetValueOrDefault(1); } // Initialize a task monitor for the download process TaskProgressMonitor baseMonitor = new TaskProgressMonitor(this, StringResources.DownloadingAudio, new CancellationTokenSource()); IAbsoluteProgressMonitor absoluteMonitor = new AbsoluteProgressMonitor(baseMonitor, totalDeploymentCost, StringResources.DownloadingAudio); // Start a separate task for downloading the files Task <List <AudioDeploymentResult> > task = Task.Factory.StartNew(() => { absoluteMonitor.SetIndeterminate(); List <AudioDeploymentResult> downloadResults = new List <AudioDeploymentResult>(); // Go through all results foreach (IDeployableAudioFile deployableFile in filesToBeDeployed) { // Download each one downloadResults.Add(DeployFile(deployableFile, absoluteMonitor, targetDirectoryProvider)); absoluteMonitor.IncreaseProgress(deployableFile.DeploymentCost.GetValueOrDefault(1)); } return(downloadResults); }); // What to do when the downloads complete task.ContinueWith((t) => { baseMonitor.Close(); // TODO: Do something with the collected DownloadResults List <AudioDeploymentResult> downloadResults = task.Result; }, System.Threading.CancellationToken.None, System.Threading.Tasks.TaskContinuationOptions.NotOnFaulted, System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext()); // What to do when the downloads fail task.ContinueWith((t) => { baseMonitor.Close(); if (t.Exception != null) { TaskHelpers.HandleTaskException(this, t.Exception, StringResources.SearchError); } }, System.Threading.CancellationToken.None, System.Threading.Tasks.TaskContinuationOptions.OnlyOnFaulted, System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext()); }
/// <summary> /// Execute a search with the given parameters (search query, page number, page size) /// The results will either be passed to ProcessSearchResults. /// </summary> /// <param name="query"></param> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <returns></returns> public Task <IEnumerable <ISearchResult> > SearchAsync(string query, string id, int pageIndex, int pageSize) { TaskProgressMonitor baseMonitor = new TaskProgressMonitor(this, StringResources.SearchingForAudio, new CancellationTokenSource()); IAbsoluteProgressMonitor absoluteMonitor = new AbsoluteProgressMonitor(baseMonitor, 1, StringResources.SearchingForAudio); int?totalNumberOfResults = null; Task <IEnumerable <ISearchResult> > task = Task.Factory.StartNew(() => { absoluteMonitor.SetIndeterminate(); // Ask the AudioSource for (async) search results try { if (!String.IsNullOrEmpty(query)) { Task <IEnumerable <ISearchResult> > searchSubtask = this.m_selectedAudioSource.Search(query, pageSize, pageIndex, absoluteMonitor, out totalNumberOfResults); return(searchSubtask.Result); } else if (!String.IsNullOrEmpty(id)) { var searchSubtask = this.m_selectedAudioSource.SearchSimilar(id, pageSize, pageIndex, absoluteMonitor, out totalNumberOfResults); return(searchSubtask.Result); } else { return(Enumerable.Empty <ISearchResult>()); } } catch (OperationCanceledException) { this.m_searchQuery = String.Empty; this.m_searchId = String.Empty; //this.searchBox.Text = this.m_searchQuery; this.m_searchPageIndex = 0; this.m_searchPageSize = pageSize; return(Enumerable.Empty <ISearchResult>()); } }); // What to do when the search fails task.ContinueWith((t) => { // Close the progress monitor baseMonitor.Close(); // If an actual exception occurred (which should be the case)... if (t.Exception != null) { // ... show an error popup TaskHelpers.HandleTaskException(this, t.Exception, StringResources.SearchError); } this.m_searchQuery = String.Empty; this.m_searchId = String.Empty; //this.searchBox.Text = this.m_searchQuery; this.m_searchPageIndex = 0; this.m_searchPageSize = pageSize; }, System.Threading.CancellationToken.None, System.Threading.Tasks.TaskContinuationOptions.OnlyOnFaulted, System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext()); // What to do when the search completes return(task.ContinueWith((t) => { // Close the progress monitor baseMonitor.Close(); // Retrieve the results IEnumerable <ISearchResult> results = task.Result; // Perform an update on the results ListView this.resultsListView.BeginUpdate(); // Process the results ProcessSearchResults(results, query, pageIndex, pageSize, totalNumberOfResults); this.resultsListView.EndUpdate(); this.m_searchQuery = query; this.m_searchId = id; //this.searchBox.Text = this.m_searchQuery; this.m_searchPageIndex = pageIndex; this.m_searchPageSize = pageSize; return results; }, System.Threading.CancellationToken.None, System.Threading.Tasks.TaskContinuationOptions.NotOnFaulted, System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext())); }