private async void MainForm_Load(object sender, EventArgs e) { #region 进度提示窗口设置 ProgressForm progressForm = new ProgressForm(); progressForm.Size = new Size(600, 80); progressForm.StartPosition = FormStartPosition.Manual; progressForm.Location = new Point(this.DesktopLocation.X + this.Width / 2 - progressForm.Size.Width / 2, this.DesktopLocation.Y + this.Height / 2 - progressForm.Size.Height / 2); progressForm.FormBorderStyle = FormBorderStyle.FixedToolWindow; progressForm.progress.Maximum = 100; progressForm.progress.Minimum = 0; progressForm.progress.Value = 0; progressForm.Show(this); this.Enabled = false; #endregion using (StreamReader reader = new StreamReader("TitleUrl.txt")) { while (!reader.EndOfStream) { string title = await reader.ReadLineAsync(); string url = await reader.ReadLineAsync(); try { titleURL.Add(title, url); } catch (Exception) { // 异常: // T:System.ArgumentNullException: // key 为 null。 // // T:System.ArgumentException: // System.Collections.Generic.Dictionary`2 中已存在具有相同键的元素。 } //更新进度条 if (progressForm.progress.Value < 90) { progressForm.progress.Value++; progressForm.Text = "正在加载离线数据:" + progressForm.progress.Value + "%"; } } } progressForm.progress.Value = 100; progressForm.Text = "任务进度:" + progressForm.progress.Value + "%"; progressForm.Close(); this.Enabled = true; }
private async void btnLoad_Click(object sender, EventArgs e) { if (btnStart.Enabled == false || btnStop.Enabled == true) { MessageBox.Show("请先停止抓取!"); return; } #region 进度提示窗口设置 ProgressForm progressForm = new ProgressForm(); progressForm.Size = new Size(600, 80); progressForm.StartPosition = FormStartPosition.Manual; progressForm.Location = new Point(this.DesktopLocation.X + this.Width / 2 - progressForm.Size.Width / 2, this.DesktopLocation.Y + this.Height / 2 - progressForm.Size.Height / 2); progressForm.FormBorderStyle = FormBorderStyle.FixedToolWindow; progressForm.progress.Maximum = 100; progressForm.progress.Minimum = 0; progressForm.progress.Value = 0; progressForm.Show(this); this.Enabled = false; #endregion //先清空原来的数据 dataGridViewTitleURL.Rows.Clear(); using (StreamReader reader = new StreamReader("TitleUrl.txt")) { while (!reader.EndOfStream) { string title = await reader.ReadLineAsync(); string url = await reader.ReadLineAsync(); dataGridViewTitleURL.Rows.Add(title, url); dataGridViewTitleURL.Rows[dataGridViewTitleURL.Rows.Count - 2].HeaderCell.Value = (dataGridViewTitleURL.Rows.Count - 1).ToString(); //更新进度条 if (progressForm.progress.Value < 50) { progressForm.progress.Value++; progressForm.Text = "任务进度:" + progressForm.progress.Value + "%"; } } } urls.Clear(); urlsToBrowse.Clear(); int numOfTitles = dataGridViewTitleURL.Rows.Count-1; using (StreamReader reader = new StreamReader("Urls.txt")) { while (!reader.EndOfStream) { string url = await reader.ReadLineAsync(); urls.Add(url); numOfTitles--; if (numOfTitles < 0) { urlsToBrowse.Enqueue(url); } //更新进度条 if (progressForm.progress.Value < 90) { progressForm.progress.Value = progressForm.progress.Value + 1; progressForm.Text = "任务进度:" + progressForm.progress.Value + "%"; } } } if (dataGridViewTitleURL.Rows.Count > 1) { string temp = dataGridViewTitleURL.Rows[0].Cells[1].Value.ToString(); txtWebSite.Text = "http://" + temp.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries)[1]; websiteCopy = txtWebSite.Text; } progressForm.Close(); MessageBox.Show("载入数据完成!"); this.Enabled = true; }
private async void btnSave_Click(object sender, EventArgs e) { if (btnStart.Enabled == false || btnStop.Enabled == true) { MessageBox.Show("请先停止抓取!"); return; } #region 进度提示窗口设置 ProgressForm progressForm = new ProgressForm(); progressForm.Size = new Size(600, 80); progressForm.StartPosition = FormStartPosition.Manual; progressForm.Location = new Point(this.DesktopLocation.X + this.Width / 2 - progressForm.Size.Width / 2, this.DesktopLocation.Y + this.Height / 2 - progressForm.Size.Height / 2); progressForm.FormBorderStyle = FormBorderStyle.FixedToolWindow; progressForm.progress.Maximum = 100; progressForm.progress.Minimum = 0; progressForm.progress.Value = 0; progressForm.Show(this); this.Enabled = false; #endregion using (StreamWriter writer = new StreamWriter("TitleUrl.txt")) { for (int i = 0; i < dataGridViewTitleURL.Rows.Count - 1; i++) { string title = dataGridViewTitleURL.Rows[i].Cells[0].Value.ToString(); string url = dataGridViewTitleURL.Rows[i].Cells[1].Value.ToString(); await writer.WriteLineAsync(title); await writer.WriteLineAsync(url); //更新进度条 progressForm.progress.Value = 50 * i / dataGridViewTitleURL.Rows.Count; progressForm.Text = "任务进度:" + progressForm.progress.Value + "%"; } } using (StreamWriter writer = new StreamWriter("Urls.txt")) { int i = 0; foreach (string url in urls) { await writer.WriteLineAsync(url); i++; //更新进度条 progressForm.progress.Value = 50 + 50 * i / urls.Count; progressForm.Text = "任务进度:" + progressForm.progress.Value + "%"; } } progressForm.Close(); MessageBox.Show("保存数据完成!"); this.Enabled = true; }