private BackgroundWorker backgroundWorker;
private void UserControlCodeOperation_Load(object sender, EventArgs e)
{initialization();checkBoxOperation.Enabled = false;checkBoxCommand.Enabled = false;backgroundWorker = new BackgroundWorker{WorkerReportsProgress = true,WorkerSupportsCancellation = true};backgroundWorker.DoWork += BackgroundWorker_DoWork;backgroundWorker.ProgressChanged += BackgroundWorker_ProgressChanged;backgroundWorker.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;}
int count = 100;
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{if (codeLines.Count > 0){count = codeLines.Count;}for (int i = 0; i < count; i++){if (backgroundWorker.CancellationPending){e.Cancel = true;break;}backgroundWorker.ReportProgress(i, codeLines[i]);}
}private void StartBackgroundWork()
{progressBar1.Visible = true;progressBar1.Value = 0;LoadImg.Left = this.Width / 2 - 38;LoadImg.Visible = true;string DateTimeNow = String.Format("{0} {1} {2}", DateTime.Now, "StartBackgroundWork", Environment.NewLine);string dirPath = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);string fileName = String.Format("log_{0:yyyyMMdd}.log", DateTime.Now);string filePath = System.IO.Path.Combine(dirPath, "log", fileName);AnalysisCodeClass.CodeFile.WriteLog(filePath, DateTimeNow);if (!backgroundWorker.IsBusy){backgroundWorker.RunWorkerAsync();}
}private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{Console.Write(e.ProgressPercentage);Console.Write(e.UserState);if (e.ProgressPercentage * 100 / count > 100){progressBar1.Value = 100;}else{progressBar1.Value = e.ProgressPercentage * 100 / count;} string DateTimeNow = String.Format("{0} {1} {2}", DateTime.Now, "", Environment.NewLine);Console.Write(DateTimeNow);ClassCodeStruct.CodeLine codeLine = (ClassCodeStruct.CodeLine)e.UserState;Console.Write(codeLine.Code);DisplayCodeLines(codeLine, e.ProgressPercentage);
}private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{progressBar1.Visible = false;if (e.Cancelled){progressBar1.Visible = false;MessageBox.Show("取消成功");}else if (e.Error != null){MessageBox.Show("执行出错 " + e.Error);}else{var res = e.Result;progressBar1.Value = 100;progressBar1.Visible = false;LoadImg.Visible = false;SetComboBox();string DateTimeNow = String.Format("{0} {1} {2}", DateTime.Now, "BackgroundWorker_RunWorkerCompleted", Environment.NewLine);string dirPath = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);string fileName = String.Format("log_{0:yyyyMMdd}.log", DateTime.Now);string filePath = System.IO.Path.Combine(dirPath,"log", fileName);AnalysisCodeClass.CodeFile.WriteLog(filePath, DateTimeNow);}
}
#region 写日志文件public static void WriteLog(string fileName, string text){string path = Path.GetDirectoryName(fileName);if (!System.IO.Directory.Exists(path)){System.IO.Directory.CreateDirectory(path);}using (StreamWriter sw = new StreamWriter(fileName, true)){sw.WriteLine(text);}}#endregion