要回答你的问题,我怎样才能控制进度条,像这样
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Backgroundworker bgw = ctype(sender,backgroundworker);
Process p = new Process();
p.StartInfo.FileName = "d:\\fix.exe";
p.StartInfo.Arguments = "-l";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = false;
p.EnableRaisingEvents = true;
p.Start();
// report progress by calling reportprogress()
bgw.ReportProgress('Integer value to be passed and used for the progressbar')
p.BeginOutputReadLine();
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage.ToString();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
现在,由于 fix.exe 不在可以增加值的循环中,因此很难获得准确的 peogressbar。一个想法,如果你绝对必须拥有它,
- 将进度条最大值设置为 10
在 DoWork 事件的几个步骤中使用 te bgw.ReportProgress()
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Backgroundworker bgw = ctype(sender,backgroundworker);
Process p = new Process();
p.StartInfo.FileName = "d:\\fix.exe";
// report progress by calling reportprogress()
bgw.ReportProgress(2);
p.StartInfo.Arguments = "-l";
p.StartInfo.UseShellExecute = false;
// report progress by calling reportprogress()
bgw.ReportProgress(5);
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = false;
p.EnableRaisingEvents = true;
p.Start();
// report progress by calling reportprogress()
bgw.ReportProgress(7);
p.BeginOutputReadLine();
}
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Maximum = 10
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage.ToString();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
progressBar1.Value = 10;
}
这会在某种程度上增加进度条,但当然进度看起来总是一样的。