[英]Update progress bar in another form while task is running
**最终,我将同时运行四个任务,并具有包含四个进度条的另一种形式。 我希望每个进度条在工作任务完成时进行更新。
这就是我要为初学者做的事情。
我有一个上面有一些按钮的表格。 当我单击一个时,我正在创建一个新任务来做一些工作。
public partial class MyMainForm : Form
{
private void btn_doWork_Click(object sender, EventArgs e)
{
Task task = new Task(RunComparisons);
task.Start();
}
private void RunComparisons()
{
int progressBarValue = 0;
MyProgressBarForm pBar = new MyProgressBarForm(maxValue, "some text");
pBar.ShowDialog();
foreach(string s in nodeCollection)
{
//do some work here
progressBarValue++;
pBar.updateProgressBar(progressBarValue, "some new text");
}
pBar.BeginInvoke(new Action(() => pBar.Close()));
}
}
在另一个包含带有进度栏的表单的类中:
public partial class MyProgressBarForm : Form
{
public MyProgressBarForm(int maxValue, string textToDisplay)
{
InitializeComponent();
MyProgressBarControl.Maximum = maxValue;
myLabel.Text = textToDisplay;
}
public void updateProgressBar(int progress, string updatedTextToDisplay)
{
MyProgressBarForm.BeginInvoke(
new Action(() =>
{
MyProgressBarControl.Value = progress;
myLabel.Text = updatedTextToDisplay;
}));
}
当我单击doWork按钮时,进度条表单将显示,但不会更新。 它只是坐在那里并挂起。 如果我注释掉pBar.ShowDialog(); 那么进度条表单不会显示,但是要完成的工作可以完美完成。
在创建自己的线程时,我可以完美地工作,但是我了解了Tasks,现在我正在尝试使其以这种方式运行。 我哪里做错了?
**最终,我将同时运行四个任务,并具有包含四个进度条的另一种形式。 我希望每个进度条在工作任务完成时进行更新。
这就是我要为初学者做的事情。
我有一个上面有一些按钮的表格。 当我单击一个时,我正在创建一个新任务来做一些工作。
public partial class MyMainForm : Form
{
private void btn_doWork_Click(object sender, EventArgs e)
{
Task task = new Task(RunComparisons);
task.Start();
}
private void RunComparisons()
{
int progressBarValue = 0;
MyProgressBarForm pBar = new MyProgressBarForm(maxValue, "some text");
pBar.ShowDialog();
foreach(string s in nodeCollection)
{
//do some work here
progressBarValue++;
pBar.updateProgressBar(progressBarValue, "some new text");
}
pBar.BeginInvoke(new Action(() => pBar.Close()));
}
}
在另一个包含带有进度栏的表单的类中:
public partial class MyProgressBarForm : Form
{
public MyProgressBarForm(int maxValue, string textToDisplay)
{
InitializeComponent();
MyProgressBarControl.Maximum = maxValue;
myLabel.Text = textToDisplay;
}
public void updateProgressBar(int progress, string updatedTextToDisplay)
{
MyProgressBarForm.BeginInvoke(
new Action(() =>
{
MyProgressBarControl.Value = progress;
myLabel.Text = updatedTextToDisplay;
}));
}
当我单击doWork按钮时,进度条表单将显示,但不会更新。 它只是坐在那里并挂起。 如果我注释掉pBar.ShowDialog(); 那么进度条表单不会显示,但是要完成的工作可以完美完成。
在创建自己的线程时,我可以完美地工作,但是我了解了Tasks,现在我正在尝试使其以这种方式运行。 我哪里做错了?
**最终,我将同时运行四个任务,并具有包含四个进度条的另一种形式。 我希望每个进度条在工作任务完成时进行更新。
这就是我要为初学者做的事情。
我有一个上面有一些按钮的表格。 当我单击一个时,我正在创建一个新任务来做一些工作。
public partial class MyMainForm : Form
{
private void btn_doWork_Click(object sender, EventArgs e)
{
Task task = new Task(RunComparisons);
task.Start();
}
private void RunComparisons()
{
int progressBarValue = 0;
MyProgressBarForm pBar = new MyProgressBarForm(maxValue, "some text");
pBar.ShowDialog();
foreach(string s in nodeCollection)
{
//do some work here
progressBarValue++;
pBar.updateProgressBar(progressBarValue, "some new text");
}
pBar.BeginInvoke(new Action(() => pBar.Close()));
}
}
在另一个包含带有进度栏的表单的类中:
public partial class MyProgressBarForm : Form
{
public MyProgressBarForm(int maxValue, string textToDisplay)
{
InitializeComponent();
MyProgressBarControl.Maximum = maxValue;
myLabel.Text = textToDisplay;
}
public void updateProgressBar(int progress, string updatedTextToDisplay)
{
MyProgressBarForm.BeginInvoke(
new Action(() =>
{
MyProgressBarControl.Value = progress;
myLabel.Text = updatedTextToDisplay;
}));
}
当我单击doWork按钮时,进度条表单将显示,但不会更新。 它只是坐在那里并挂起。 如果我注释掉pBar.ShowDialog(); 那么进度条表单不会显示,但是要完成的工作可以完美完成。
在创建自己的线程时,我可以完美地工作,但是我了解了Tasks,现在我正在尝试使其以这种方式运行。 我哪里做错了?
**最终,我将同时运行四个任务,并具有包含四个进度条的另一种形式。 我希望每个进度条在工作任务完成时进行更新。
这就是我要为初学者做的事情。
我有一个上面有一些按钮的表格。 当我单击一个时,我正在创建一个新任务来做一些工作。
public partial class MyMainForm : Form
{
private void btn_doWork_Click(object sender, EventArgs e)
{
Task task = new Task(RunComparisons);
task.Start();
}
private void RunComparisons()
{
int progressBarValue = 0;
MyProgressBarForm pBar = new MyProgressBarForm(maxValue, "some text");
pBar.ShowDialog();
foreach(string s in nodeCollection)
{
//do some work here
progressBarValue++;
pBar.updateProgressBar(progressBarValue, "some new text");
}
pBar.BeginInvoke(new Action(() => pBar.Close()));
}
}
在另一个包含带有进度栏的表单的类中:
public partial class MyProgressBarForm : Form
{
public MyProgressBarForm(int maxValue, string textToDisplay)
{
InitializeComponent();
MyProgressBarControl.Maximum = maxValue;
myLabel.Text = textToDisplay;
}
public void updateProgressBar(int progress, string updatedTextToDisplay)
{
MyProgressBarForm.BeginInvoke(
new Action(() =>
{
MyProgressBarControl.Value = progress;
myLabel.Text = updatedTextToDisplay;
}));
}
当我单击doWork按钮时,进度条表单将显示,但不会更新。 它只是坐在那里并挂起。 如果我注释掉pBar.ShowDialog(); 那么进度条表单不会显示,但是要完成的工作可以完美完成。
在创建自己的线程时,我可以完美地工作,但是我了解了Tasks,现在我正在尝试使其以这种方式运行。 我哪里做错了?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.