后台工作人员和进度条随着外部 exe 的进度而动画化

本文关键字:exe 外部 动画 工作人员 后台 | 更新日期: 2023-09-27 18:33:57

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

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

后台工作人员和进度条随着外部 exe 的进度而动画化

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

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. 使用 te bgw。报告进度() 在 DoWork 事件中的几个步骤

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

这将在某种程度上增加进度条,但当然进度将始终相同。