private void GETbutton_Click(object sender, EventArgs e) { string filenamePattern = FilenamePatterncomboBox.Text; GETbutton.Enabled = false; StatusInfolabel.Text = "StatusInfo"; StatusPDFinishedCountlabel.Text = "0"; StatusPDTotalCountlabel.Text = "0"; Stopwatch sw = new Stopwatch(); sw.Start(); int id = Convert.ToInt32(IDtextBox.Text); int modelIndex = Convert.ToInt32(LyricsStylenumericUpDown.Value); int delayMsec = Convert.ToInt32(DelayMsecnumericUpDown.Value); if (MusicradioButton.Checked) { Music m = new Music(id); string Log = ""; LyricsStatus status = LyricsStatus.UNSURED; if (ReviseRawcheckBox.Checked == false) { Log = DownloadLrc(".\\", filenamePattern, m, modelIndex, delayMsec, out status, out string filePath);//正常的自动化操作 } else { HttpRequest hr = new HttpRequest(); string sContentOriLyrics = hr.GetContent("https://music.163.com/api/song/media?id=" + id); Clipboard.SetText(sContentOriLyrics, TextDataFormat.UnicodeText); if (MessageBox.Show("OriLyrics raw text has been set is in the clipboard. Please follow these guide below." + Environment.NewLine + Environment.NewLine + "==========" + Environment.NewLine + @"1. Use Notepad++ to read(such as ""paste"") text from the clipboard." + Environment.NewLine + @"2. Try to revise the text. The most mistake we face is wrong typo. (You can search "".lrc guidance"" to learn more.)" + Environment.NewLine + @"3. After revising, please set revised text to the clipboard(such as ""copy"")" + Environment.NewLine + @"4. Click ""Yes"" button. Software will use the revised text instead of the raw text." + Environment.NewLine + "==========" + Environment.NewLine + Environment.NewLine + @"**If you don't know why you're seeing this messagebox now or you don't know what to do, just click ""No"" button. Don't worry, nothing wrong will happen.**" + Environment.NewLine + @"You can click the blue label ""Need Help?"" in the LrcDownloader Form to learn more about this function.", "OriLyrics raw text - ReviseRaw(Func)_Step 1", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.Yes) { sContentOriLyrics = Clipboard.GetText(TextDataFormat.UnicodeText); } string sContentTransLyrics = hr.GetContent("https://music.163.com/api/song/lyric?os=pc&id=" + id + "&tv=-1"); Clipboard.SetText(sContentTransLyrics, TextDataFormat.UnicodeText); if (MessageBox.Show("TransLyrics raw text has been set is in the clipboard. Please follow these guide below." + Environment.NewLine + Environment.NewLine + "==========" + Environment.NewLine + @"1. Use Notepad++ to read(such as ""paste"") text from the clipboard." + Environment.NewLine + @"2. Try to revise the text. The most mistake we face is wrong typo. (You can search "".lrc guidance"" to learn more.)" + Environment.NewLine + @"3. After revising, please set revised text to the clipboard(such as ""copy"")" + Environment.NewLine + @"4. Click ""Yes"" button. Software will use the revised text instead of the raw text." + Environment.NewLine + "==========" + Environment.NewLine + Environment.NewLine + @"**If you don't know why you're seeing this messagebox now or you don't know what to do, just click ""No"" button. Don't worry, nothing wrong will happen.**" + Environment.NewLine + @"You can click the blue label ""Need Help?"" in the LrcDownloader Form to learn more about this function.", "TransLyrics raw text - ReviseRaw(Func)_Step 2", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.Yes) { sContentTransLyrics = Clipboard.GetText(TextDataFormat.UnicodeText); } Log = DownloadLrc(".\\", filenamePattern, m, modelIndex, delayMsec, out status, out string filePath, revisedsContentOriLyricsForUserReviseFunc: sContentOriLyrics, revisedsContentTransLyricsForUserReviseFunc: sContentTransLyrics); } sw.Stop(); if (Log == "") { StatusInfolabel.Text = "Done Status:" + status + "\r\nUsed Time:" + Math.Round(sw.Elapsed.TotalSeconds, 3) + "sec"; } else { StatusInfolabel.Text = Log + " Status:" + status + "\r\nUsed Time:" + Math.Round(sw.Elapsed.TotalSeconds, 3) + "sec"; } GETbutton.Enabled = true; IDtextBox.Clear(); } else if (PlaylistradioButton.Checked) { cancelToken = new CancellationTokenSource(); ParallelOptions parOpts = new ParallelOptions() { CancellationToken = cancelToken.Token, MaxDegreeOfParallelism = System.Environment.ProcessorCount//上面三行针对TPL并行类库 }; Task.Factory.StartNew(() => { Playlist pl = new Playlist(id); LogFileWriter logWriter = new LogFileWriter(@".\\" + FormatFileName.CleanInvalidFileName(pl.Name) + @"\"); List <long> idList = pl.SongidInPlaylist; logWriter.AppendLyricsDownloadTaskDetail(); logWriter.AppendLyricsDownloadTaskDetail(pl.SongidInPlaylist.Count); this.Invoke((Action) delegate { StatusPDTotalCountlabel.Text = idList.Count.ToString(); //写Status Cancelbutton.Enabled = true; //下面使用TPL类库,可以取消 }); try { Parallel.For(0, idList.Count, parOpts, i => { parOpts.CancellationToken.ThrowIfCancellationRequested(); string ErrorLog = ""; LyricsStatus status = LyricsStatus.UNSURED; string filePath = ""; Music m = new Music(idList[i], i + 1); try { ErrorLog = DownloadLrc(@".\\" + FormatFileName.CleanInvalidFileName(pl.Name) + @"\", filenamePattern, m, modelIndex, delayMsec, out status, out filePath); } catch (Exception ex) { ErrorLog += "<" + ex.Message + ">"; } finally { if (System.IO.File.Exists(filePath)) { logWriter.AppendLyricsDownloadTaskDetail(i + 1, idList[i], m.Title, status.ToString(), "√" + ErrorLog); } else { logWriter.AppendLyricsDownloadTaskDetail(i + 1, idList[i], m.Title, status.ToString(), "×" + ErrorLog); } this.Invoke((Action) delegate { StatusPDFinishedCountlabel.Text = (Convert.ToInt32(StatusPDFinishedCountlabel.Text) + 1).ToString(); }); m = null; } }); } catch (OperationCanceledException) { // } finally { this.Invoke((Action) delegate { Cancelbutton.Enabled = false; GETbutton.Enabled = true; IDtextBox.Clear(); }); } sw.Stop(); logWriter.AppendHeadInformation("Playlist", id, pl.Name, idList.Count, cancelToken.IsCancellationRequested == true ? "Canceled" : "Finished"); logWriter.AppendBottomInformation(Math.Round(sw.Elapsed.TotalSeconds, 3)); logWriter.WriteFIle(); this.Invoke((Action) delegate { StatusInfolabel.Text = (cancelToken.IsCancellationRequested == true ? "Canceled" : "Finished") + "\r\nRead Log.txt to learn more."; }); cancelToken.Dispose(); pl = null; }); } else if (AlbumradioButton.Checked) { cancelToken = new CancellationTokenSource(); ParallelOptions parOpts = new ParallelOptions() { CancellationToken = cancelToken.Token, MaxDegreeOfParallelism = System.Environment.ProcessorCount//上面三行针对TPL并行类库 }; Task.Factory.StartNew(() => { Album a = new Album(id); LogFileWriter logWriter = new LogFileWriter(@".\\" + FormatFileName.CleanInvalidFileName(a.Name) + @"\"); List <long> idList = a.SongidInAlbum; logWriter.AppendLyricsDownloadTaskDetail(); logWriter.AppendLyricsDownloadTaskDetail(a.SongidInAlbum.Count); this.Invoke((Action) delegate { StatusPDTotalCountlabel.Text = idList.Count.ToString(); //写Status Cancelbutton.Enabled = true; //下面使用TPL类库,可以取消 }); try { Parallel.For(0, idList.Count, parOpts, i => { parOpts.CancellationToken.ThrowIfCancellationRequested(); Music m = new Music(idList[i], i + 1); string ErrorLog = ""; LyricsStatus status = LyricsStatus.UNSURED; string filePath = ""; try { ErrorLog = DownloadLrc(@".\\" + FormatFileName.CleanInvalidFileName(a.Name) + @"\", filenamePattern, m, modelIndex, delayMsec, out status, out filePath); } catch (Exception ex) { ErrorLog += "<" + ex.Message + ">"; } finally { if (System.IO.File.Exists(filePath)) { logWriter.AppendLyricsDownloadTaskDetail(i + 1, idList[i], m.Title, status.ToString(), "√" + ErrorLog); } else { logWriter.AppendLyricsDownloadTaskDetail(i + 1, idList[i], m.Title, status.ToString(), "×" + ErrorLog); } this.Invoke((Action) delegate { StatusPDFinishedCountlabel.Text = (Convert.ToInt32(StatusPDFinishedCountlabel.Text) + 1).ToString(); }); m = null; } }); } catch (OperationCanceledException) { // } finally { this.Invoke((Action) delegate { Cancelbutton.Enabled = false; GETbutton.Enabled = true; IDtextBox.Clear(); }); } sw.Stop(); logWriter.AppendHeadInformation("Album", id, a.Name, idList.Count, cancelToken.IsCancellationRequested == true ? "Canceled" : "Finished"); logWriter.AppendBottomInformation(Math.Round(sw.Elapsed.TotalSeconds, 3)); logWriter.WriteFIle(); this.Invoke((Action) delegate { StatusInfolabel.Text = (cancelToken.IsCancellationRequested == true ? "Canceled" : "Finished") + "\r\nRead Log.txt to learn more."; }); cancelToken.Dispose(); a = null; }); } }
private void GETbutton_Click(object sender, EventArgs e) { string filenamePattern = FilenamePatterncomboBox.Text; GETbutton.Enabled = false; StatusInfolabel.Text = "StatusInfo"; StatusPDFinishedCountlabel.Text = "0"; StatusPDTotalCountlabel.Text = "0"; Stopwatch sw = new Stopwatch(); sw.Start(); int id = Convert.ToInt32(IDtextBox.Text); int modelIndex = Convert.ToInt32(LyricsStylenumericUpDown.Value); int delayMsec = Convert.ToInt32(DelayMsecnumericUpDown.Value); if (MusicradioButton.Checked) { Music m = new Music(id); string Log = DownloadLrc(".\\", filenamePattern, m, modelIndex, delayMsec, out LyricsStatus status, out string filePath); sw.Stop(); if (Log == "") { StatusInfolabel.Text = "Done Status:" + status + "\r\nUsed Time:" + Math.Round(sw.Elapsed.TotalSeconds, 3) + "sec"; } else { StatusInfolabel.Text = Log + " Status:" + status + "\r\nUsed Time:" + Math.Round(sw.Elapsed.TotalSeconds, 3) + "sec"; } GETbutton.Enabled = true; IDtextBox.Clear(); } else if (PlaylistradioButton.Checked) { cancelToken = new CancellationTokenSource(); ParallelOptions parOpts = new ParallelOptions() { CancellationToken = cancelToken.Token, MaxDegreeOfParallelism = System.Environment.ProcessorCount//上面三行针对TPL并行类库 }; Task.Factory.StartNew(() => { Playlist pl = new Playlist(id); LogFileWriter logWriter = new LogFileWriter(@".\\" + pl.Name + @"\"); List <long> idList = pl.SongidInPlaylist; logWriter.AppendLyricsDownloadTaskDetail(); logWriter.AppendLyricsDownloadTaskDetail(pl.SongidInPlaylist.Count); this.Invoke((Action) delegate { StatusPDTotalCountlabel.Text = idList.Count.ToString(); //写Status Cancelbutton.Enabled = true; //下面使用TPL类库,可以取消 }); try { Parallel.For(0, idList.Count, parOpts, i => { parOpts.CancellationToken.ThrowIfCancellationRequested(); string ErrorLog = ""; LyricsStatus status = LyricsStatus.UNSURED; string filePath = ""; Music m = new Music(idList[i], i + 1); try { ErrorLog = DownloadLrc(".\\" + pl.Name + @"\", filenamePattern, m, modelIndex, delayMsec, out status, out filePath); } catch (Exception ex) { ErrorLog += "<" + ex.Message + ">"; } finally { if (System.IO.File.Exists(filePath)) { logWriter.AppendLyricsDownloadTaskDetail(i + 1, idList[i], m.Title, status.ToString(), "√" + ErrorLog); } else { logWriter.AppendLyricsDownloadTaskDetail(i + 1, idList[i], m.Title, status.ToString(), "×" + ErrorLog); } this.Invoke((Action) delegate { StatusPDFinishedCountlabel.Text = (Convert.ToInt32(StatusPDFinishedCountlabel.Text) + 1).ToString(); }); m = null; } }); } catch (OperationCanceledException) { // } finally { this.Invoke((Action) delegate { Cancelbutton.Enabled = false; GETbutton.Enabled = true; IDtextBox.Clear(); }); } sw.Stop(); logWriter.AppendHeadInformation("Playlist", id, pl.Name, idList.Count, cancelToken.IsCancellationRequested == true ? "Canceled" : "Finished"); logWriter.AppendBottomInformation(Math.Round(sw.Elapsed.TotalSeconds, 3)); logWriter.WriteFIle(); this.Invoke((Action) delegate { StatusInfolabel.Text = (cancelToken.IsCancellationRequested == true ? "Canceled" : "Finished") + "\r\nRead Log.txt to learn more."; }); cancelToken.Dispose(); pl = null; }); } else if (AlbumradioButton.Checked) { cancelToken = new CancellationTokenSource(); ParallelOptions parOpts = new ParallelOptions() { CancellationToken = cancelToken.Token, MaxDegreeOfParallelism = System.Environment.ProcessorCount//上面三行针对TPL并行类库 }; Task.Factory.StartNew(() => { Album a = new Album(id); LogFileWriter logWriter = new LogFileWriter(@".\\" + a.Name + @"\"); List <long> idList = a.SongidInAlbum; logWriter.AppendLyricsDownloadTaskDetail(); logWriter.AppendLyricsDownloadTaskDetail(a.SongidInAlbum.Count); this.Invoke((Action) delegate { StatusPDTotalCountlabel.Text = idList.Count.ToString(); //写Status Cancelbutton.Enabled = true; //下面使用TPL类库,可以取消 }); try { Parallel.For(0, idList.Count, parOpts, i => { parOpts.CancellationToken.ThrowIfCancellationRequested(); Music m = new Music(idList[i], i + 1); string ErrorLog = ""; LyricsStatus status = LyricsStatus.UNSURED; string filePath = ""; try { ErrorLog = DownloadLrc(".\\" + a.Name + @"\", filenamePattern, m, modelIndex, delayMsec, out status, out filePath); } catch (Exception ex) { ErrorLog += "<" + ex.Message + ">"; } finally { if (System.IO.File.Exists(filePath)) { logWriter.AppendLyricsDownloadTaskDetail(i + 1, idList[i], m.Title, status.ToString(), "√" + ErrorLog); } else { logWriter.AppendLyricsDownloadTaskDetail(i + 1, idList[i], m.Title, status.ToString(), "×" + ErrorLog); } this.Invoke((Action) delegate { StatusPDFinishedCountlabel.Text = (Convert.ToInt32(StatusPDFinishedCountlabel.Text) + 1).ToString(); }); m = null; } }); } catch (OperationCanceledException) { // } finally { this.Invoke((Action) delegate { Cancelbutton.Enabled = false; GETbutton.Enabled = true; IDtextBox.Clear(); }); } sw.Stop(); logWriter.AppendHeadInformation("Album", id, a.Name, idList.Count, cancelToken.IsCancellationRequested == true ? "Canceled" : "Finished"); logWriter.AppendBottomInformation(Math.Round(sw.Elapsed.TotalSeconds, 3)); logWriter.WriteFIle(); this.Invoke((Action) delegate { StatusInfolabel.Text = (cancelToken.IsCancellationRequested == true ? "Canceled" : "Finished") + "\r\nRead Log.txt to learn more."; }); cancelToken.Dispose(); a = null; }); } }