如何根据功能竞争来增加WinForms进度栏?

时间:2019-02-20 23:43:57

标签: c# windows visual-studio winforms progress-bar

我无法增加进度栏。我希望它在每个函数结束后增加某个值。我看过的所有示例都有不带任何参数的函数。这就是我到目前为止

    public partial class TaskProgress: Form
        {
            public TaskProgress(string parameter, string parameter2, string parameter3)
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, System.EventArgs e)
            {
                // Start the BackgroundWorker.
               backgroundWorker1.WorkerReportsProgress = true;
                progressBar1.Value = 0;
                backgroundWorker1.RunWorkerAsync();
            }

            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                /*for (int i = 1; i <= 100; i++)
                {    Thread.Sleep(100);
                    backgroundWorker1.ReportProgress(i);}*/

                  function1(parameter1);
                  backgroundWorker1.ReportProgress(20);
                  function2(parameter2);
                  backgroundWorker1.ReportProgress(60);
                  function3(parameter3);
                  backgroundWorker1.ReportProgress(100);
            }

            private void backgroundWorker1_ProgressChanged(object sender,
                ProgressChangedEventArgs e)
            {
                // Change the value of the ProgressBar to the BackgroundWorker progress.
                progressBar1.Value = e.ProgressPercentage;
                // Set the text.
                this.Text = "Progress: " + e.ProgressPercentage.ToString() + "%";
            }
    }

如何将参数值传递给function1,function2和function3?现在将Parameter1,2,3传递给function1,2,3的方式是错误的,因为这些值未在backgroundworker函数中定义。参数1,2,3是动态的,并在构造TaskProgress类时传递。

或者有其他方法可以根据我的功能竞争来增加进度条吗?

我看了这个示例,但是backgroundworker_dowork()内部的函数不需要其他参数How to use WinForms progress Bar?

我无法致电ReportProgress();由于我的Form.cs [Design]代码中包含以下代码,因此在backgroundWorker1_DoWork外部运行:

  // 
  this.backgroundWorker1.WorkerReportsProgress = true;
  this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
  this.backgroundWorker1.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged);
  // 

我也无法更改backgroundWorker1_DoWork()接受的参数数量

以前我是

      public partial class TaskProgress: Form
        {
            public TaskProgress(string parameter, string parameter2, string parameter3)
            {
                InitializeComponent();
                function1(parameter1);
                function2(parameter2);
                function3(parameter3);
            }

            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                for (int i = 1; i <= 100; i++)
                {    Thread.Sleep(100);
                    backgroundWorker1.ReportProgress(i);}
            }
       }

但是进度条本身就移动了;与功能何时完成无关

1 个答案:

答案 0 :(得分:0)

在我看来,您最简单的方法是这样做:

public partial class TaskProgress : Form
{
    private string parameter = null;
    private string parameter2 = null;
    private string parameter3 = null;

    public TaskProgress(string parameter, string parameter2, string parameter3)
    {
        InitializeComponent();
        this.parameter = parameter;
        this.parameter2 = parameter2;
        this.parameter3 = parameter3;
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        function1(this.parameter1);
        backgroundWorker1.ReportProgress(20);
        function2(this.parameter2);
        backgroundWorker1.ReportProgress(60);
        function3(this.parameter3);
        backgroundWorker1.ReportProgress(100);
    }
}