基本上我想更新FormMain(WindowsForm)上的ProgressBar UI对象。我使用.NET 4.0从任务并行库更新ProgressBar UI对象
以下是在Form1.designer.cs
namespace ProgressBarApp
{
public partial class Form1 : Form
{
private System.Windows.Forms.ProgressBar curProgressBar;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
CustomProcess theProcess = new CustomProcess();
theProcess.Process();
}
}
}
下面的代码是CustomProcess.cs的定义
namespace ProgressBarApp
{
class CustomProcess
{
public void Process()
{
for (int i = 0; i < 10; i++)
{
Task ProcessATask = Task.Factory.StartNew(() =>
{
Thread.Sleep(1000); // simulating a process
}
);
Task UpdateProgressBar = ProcessATask.ContinueWith((antecedent) =>
{
// how do i update the progress bar object at UI here ?
}
);
}
}
}
}
你应该更新内部ProcessATask进度,否则将立即填满。因为您使用独立的任务来更新进度条。 – fofik
执行UpdateProgressBar完成ProcessATask之后。这是使用.ContinueWith定义的。我需要等到上一个任务完成,以便我可以报告此任务全部完成。 –