基本目标是我有四个进度条,并且希望在按下按钮时立即运行它们,并且我不必使用后台工作人员必须这样做。
var t = new Thread(() =>
{
try
{
}
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
我尝试并编纂了
for (i = 0; i < 4; i++)
{
var t = new Thread(() =>
{
for (double x = 0; x < 10000; x = x + 0.5)
{
progressVal=(int)x;
this.Invoke(new EventHandler(ProgressBar));
Thread.Sleep(2);
}
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
private void ProgressBar(object sender, EventArgs e)
{
progressBar1.Value=progressVal;
}
但是想不出如何制作其他进度条的想法
答案 0 :(得分:4)
我会将进度条放入数组中:
var pBars = new[] { progressBar1, progressBar2, progressBar3, progressBar4 };
foreach (var pBar in pBars)
{
new Thread(currentPBar =>
{
for (double x = 0; x < 10000; x = x + 0.5)
{
var progress = (int)x;
Action<ProgressBar, int> del = UpdateProgress;
Invoke(
del,
new object[] { (ProgressBar)currentPBar, progress }
);
Thread.Sleep(2);
}
}).Start(pBar);
}
和UpdateProgress
方法:
private void UpdateProgress(ProgressBar pBar, int progress)
{
pBar.Value = progress;
}
话虽如此,使用BackgroundWorker更适合您的场景。