Report Parallel.For progress
I had a for loop that i converted into a parallel for loop which works fine.
I'd like to be able to report progress too. I'm guessing i'll have to stop using the parallel.for and set up each loop iteration as a task, then report progress when any task finishs, baseing the progress reported on the number of tasks still running.
I'm not too sure how i go about this though, or whether there's a better way.
P.S. although i'm using .net 3.5 with the parallel extensions... where's the 4.0 prefix!
Re: Report Parallel.For progress
On a form with a listbox, progress bar & button...
The background task allows us to continuously update the UI: not having it results in a "flood" of updates towards the end of the process.
Code:
private static readonly Random RandomWaitTime = new Random();
private int _iterations;
private int _completedIterations;
private void button1_Click(object sender, EventArgs e)
{
if (!int.TryParse(textBox1.Text, out _iterations))
{
textBox1.Text = "100";
_iterations = 100;
}
ResetState();
ResetProgress();
Task.Factory.StartNew(StartLooping);
}
private void StartLooping()
{
Parallel.For(0, _iterations, ParallelTask);
}
private void ParallelTask(int taskId, ParallelLoopState loopState)
{
WaitForAWhile();
ReportTaskComplete(taskId);
}
private void ReportTaskComplete(long taskId)
{
_completedIterations++;
if (listBox1.InvokeRequired)
listBox1.BeginInvoke((Action) (() => UpdateProgress(taskId)));
else
UpdateProgress(taskId);
}
private void UpdateProgress(long completedTaskId)
{
int newItemIndex = listBox1.Items.Add(completedTaskId.ToString());
listBox1.SelectedIndex = newItemIndex;
progressBar1.Value = _completedIterations;
}
private static void WaitForAWhile()
{
int waitMillis = RandomWaitTime.Next(501);
Thread.Sleep(waitMillis);
}
private void ResetState()
{
_completedIterations = 0;
listBox1.Items.Clear();
}
private void ResetProgress()
{
progressBar1.Value = 0;
progressBar1.Maximum = _iterations;
}
Re: Report Parallel.For progress
I'm not sure that reporting progress on such an operation is worthwhile. Performing the tasks serially would result in fairly uniform stepping of the progress. Performing the the tasks in parrallel should, ideally, result in them all finishing at approximately the same time, so your progress would be zero for a long time and then suddenly be complete. Is that really what you want to show your users?
Re: Report Parallel.For progress
Thanks axion_sa, i should be able to take that and work it into what i have currently.
Quote:
Originally Posted by
jmcilhinney
I'm not sure that reporting progress on such an operation is worthwhile. Performing the tasks serially would result in fairly uniform stepping of the progress. Performing the the tasks in parrallel should, ideally, result in them all finishing at approximately the same time, so your progress would be zero for a long time and then suddenly be complete. Is that really what you want to show your users?
Ideally they'd all finish at the same time, but since there will always be many more tasks than theads to perform those tasks then i think reporting progress as each task is completed should work adequately.