0

我在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)
    {

    } 

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

4

1 回答 1

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

  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;
        } 
    

这会在某种程度上增加进度条,但当然进度看起来总是一样的。

于 2013-10-04T07:36:49.080 回答