Backgroundworker和Progress bar动画与外部exe的进展

时间:2013-10-03 14:38:05

标签: c# progress-bar backgroundworker

我在 Visual Studio 2010 中调用C#中的外部exe。我有一个背景工作者和一个进度条。我想动画进度条,开始执行外部exe到exe的执行结束。我的代码是

 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {

        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();
        p.BeginOutputReadLine();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        backgroundWorker1.RunWorkerAsync();
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {

    } 

我如何控制进度条,请指导我

1 个答案:

答案 0 :(得分:0)

要回答您的问题,我如何控制进度条,例如

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。一个想法,如果你absolutley必须拥有它,

  1. 将进度条最大值设置为10
  2. 在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;
        } 
    
  3. 这会以某种方式增加进度条,但进展总是看起来一样。