csharp
复制代码
private BackgroundWorker backgroundWorker;
private void UserControlCodeOperation_Load(object sender, EventArgs e)
{
initialization();//初始化
checkBoxOperation.Enabled = false;
checkBoxCommand.Enabled = false;
// 初始化 BackgroundWorker
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]);
//System.Threading.Thread.Sleep(10); // 模拟操作时间
}
}
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();
//MessageBox.Show("操作完成 " + res);
//MessageBox.Show("操作完成 " + DateTime.Now);
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);
}
}