|
-
Jun 23rd, 2010, 10:54 AM
#1
Thread Starter
Not NoteMe
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!
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
-
Jun 23rd, 2010, 01:19 PM
#2
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;
}
-
Jun 23rd, 2010, 07:23 PM
#3
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?
-
Jun 24th, 2010, 03:47 AM
#4
Thread Starter
Not NoteMe
Re: Report Parallel.For progress
Thanks axion_sa, i should be able to take that and work it into what i have currently.
 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.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe
"I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Have I helped you? Please Rate my posts. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|