void TrackDownloadClient_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e) { if (e.Cancelled) { TaskbarUtils.SetProgressState(Handle, TaskbarUtils.ProgressState.NoProgress); SetFormEnabled(true); SetDataGridRowColor(Color.LightCoral, null, DownloadData.CurrentTrack.Title); } else if (e.Error != null) { TaskbarUtils.SetProgressState(Handle, TaskbarUtils.ProgressState.Error); TaskbarUtils.Flash(Handle); ShowEx("Failed to download track", e.Error); TaskbarUtils.SetProgressState(Handle, TaskbarUtils.ProgressState.NoProgress); SetFormEnabled(true); } else { if (DownloadData.Step++ == 0) { string page = Encoding.ASCII.GetString(e.Result); var match = TrackUriRegex.Match(page); if (match.Success) { var filepath = match.Groups["file"].Value; var index = filepath.LastIndexOf('/'); if (index >= 0) { DownloadData.Filename = Uri.UnescapeDataString(filepath.Substring(index + 1)); TrackDownloadClient.DownloadDataAsync(new Uri(filepath)); } } } else { // write track to disk File.WriteAllBytes(Path.Combine(DownloadData.FolderPath, DownloadData.Filename), e.Result); // mark track in list as finished SetDataGridRowColor(ProgressTrackProgressBar.ForeColor, null, DownloadData.CurrentTrack.Title); // download next StartDownload(); } } }
private void StartDownload() { if (DownloadData.RemainingTracks.Count > 0) { DownloadData.CurrentTrack = DownloadData.RemainingTracks[0]; DownloadData.RemainingTracks.RemoveAt(0); DownloadData.TrackBytes = 0; DownloadData.Step = 0; UpdateProgressUI(); TrackDownloadClient.DownloadDataAsync(DownloadData.CurrentTrack.Address); } else { ProgressOverallValueLabel.Text = ProgressTrackValueLabel.Text = "100 %"; ProgressOverallProgressBar.Value = ProgressTrackProgressBar.Value = 100; SetFormEnabled(true); TaskbarUtils.SetProgressState(Handle, TaskbarUtils.ProgressState.NoProgress); TaskbarUtils.Flash(Handle); } }