【发布时间】:2019-07-14 18:33:20
【问题描述】:
我无法增加进度条。我希望它在每个函数结束后增加一个特定的值。我看过的所有示例都有不接受任何参数的函数。这就是我目前所拥有的
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);}
}
}
但是进度条自己移动了;独立于函数何时完成
【问题讨论】:
-
您的问题中定义的
function1(parameter1)等在哪里?你应该展示你的完整代码。 -
"是否有另一种方法可以根据我的功能增加我的进度条?"当然!你可以在你的函数中调用
backgroundWorker1.ReportProgress();...这没有错! -
"如何将参数值传递给函数 1、函数 2 和函数 3?" - 您的代码已经准确显示了如何将参数值传递给这些函数。
-
“根据我的功能增加我的进度条”是什么意思?
-
感谢所有帮助,我已将我的问题编辑得更具体并包含更多信息
标签: c# windows visual-studio winforms progress-bar