private bool IsExceptionHandled(string title, Exception ex, DownloadOperation download = null) { WebErrorStatus error = BackgroundTransferError.GetStatus(ex.HResult); if (error == WebErrorStatus.Unknown) { return(false); } string errorMsg = null; if (download == null) { Debug.WriteLine(String.Format(CultureInfo.CurrentCulture, "Error: {0}: {1}", title, error)); errorMsg = String.Format(CultureInfo.CurrentCulture, "{0}: {1}", title, error); } else { Debug.WriteLine(String.Format(CultureInfo.CurrentCulture, "Error: {0} - {1}: {2}", download.Guid, title, error)); errorMsg = String.Format(CultureInfo.CurrentCulture, "{0} - {1}: {2}", download.Guid, title, error); } // Event calling for status msg DownloadStatusMessageChanged?.Invoke("Error", download.ResultFile.Name); return(true); }
private async Task HandleDownloadAsync(DownloadOperation download, bool start) { try { Debug.WriteLine("Running: " + download.Guid); // Store the download so we can pause/resume. activeDownloads.Add(download); Progress <DownloadOperation> progressCallback = new Progress <DownloadOperation>(DownloadProgress); if (start) // Start the download and attach a progress handler. { await download.StartAsync().AsTask(cts.Token, progressCallback); } else // The download was already running when the application started, re-attach the progress handler. { await download.AttachAsync().AsTask(cts.Token, progressCallback); } ResponseInformation response = download.GetResponseInformation(); Debug.WriteLine(String.Format(CultureInfo.CurrentCulture, "Completed: {0}, Status Code: {1}", download.Guid, response.StatusCode)); // Move the downloaded file from Temporary folder to the selected folder if (StorageFileDictionary.ContainsKey(download.ResultFile.Name)) { StorageFile file_selectedLocation = StorageFileDictionary[download.ResultFile.Name]; // convert item to mp3 first if (IsMusicDictionary[download.ResultFile.Name]) { Debug.WriteLine("Converting video to mp3: " + download.Guid); // Others in [File] string OriginalFileName = download.ResultFile.Name; AudioEncodingQuality audioQuality = AudioQualityDictionary[download.ResultFile.Name]; bool ism4a = IsisM4ADictionary[download.ResultFile.Name]; IStorageFile result = null; VideoMP3Transcoder extractor = new VideoMP3Transcoder(OriginalFileName, download.ResultFile, audioQuality, ism4a); result = await Task.Run(async() => await extractor.ConvertToMP3() ); Debug.WriteLine("Result: " + result != null); try { await result.MoveAndReplaceAsync(file_selectedLocation); } catch (Exception e) { Debug.WriteLine(e.Message.ToString()); } if (result != null) { Debug.WriteLine("Done converting to mp3: " + download.Guid); } else { Debug.WriteLine("Error converting to mp3: " + download.Guid); } } else { await download.ResultFile.MoveAndReplaceAsync(StorageFileDictionary[download.ResultFile.Name]); } } } catch (TaskCanceledException) { Debug.WriteLine("Canceled: " + download.Guid); } catch (Exception ex) { if (!IsExceptionHandled("Execution error", ex, download)) { throw; } } finally { activeDownloads.Remove(download); // Event calling for status msg DownloadStatusMessageChanged?.Invoke("Download complete", download.ResultFile.Name); } }