ProgressBar in multi-threaded application
-
How to report a calculations' progress using a
ProgressBar
in a WinForms multi-threaded application? I want to utilize CPUs as much as I can and so use all (four) cores. After some googling the only thing I found is a suggestion to use aBackgroudWorker
, but it supports only one thread for the same work. The code looks like this:Parallel.For(0, width, delegate(int x)
{
//for (int x = 0; x < width; x++)
//{
//calculations with two more nested loops.
(***)
//}
});At (***) I tried the following: 1.)
progress.Value = x;
This thrown an exception of course. 2.)
progress.BeginInvoke(new Action (delegate {
progress.Value = x;
}));Here the ProgressBar was updated after all calculation were finished. Tried to set a number of threads to 3 but it didn't change anything. The implemetation of the
Parallel
class is robust imho. Any help appreciated.Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
-
How to report a calculations' progress using a
ProgressBar
in a WinForms multi-threaded application? I want to utilize CPUs as much as I can and so use all (four) cores. After some googling the only thing I found is a suggestion to use aBackgroudWorker
, but it supports only one thread for the same work. The code looks like this:Parallel.For(0, width, delegate(int x)
{
//for (int x = 0; x < width; x++)
//{
//calculations with two more nested loops.
(***)
//}
});At (***) I tried the following: 1.)
progress.Value = x;
This thrown an exception of course. 2.)
progress.BeginInvoke(new Action (delegate {
progress.Value = x;
}));Here the ProgressBar was updated after all calculation were finished. Tried to set a number of threads to 3 but it didn't change anything. The implemetation of the
Parallel
class is robust imho. Any help appreciated.Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
There is a method that background workers have that is called "ReportProgress", and a event called "ProgressChanged" that triggers when the ReportProgress method is called. You can access the ProgressChanged from the original thread, unlike trying to directly access the progress bar via
progressbar.value = x
. -
How to report a calculations' progress using a
ProgressBar
in a WinForms multi-threaded application? I want to utilize CPUs as much as I can and so use all (four) cores. After some googling the only thing I found is a suggestion to use aBackgroudWorker
, but it supports only one thread for the same work. The code looks like this:Parallel.For(0, width, delegate(int x)
{
//for (int x = 0; x < width; x++)
//{
//calculations with two more nested loops.
(***)
//}
});At (***) I tried the following: 1.)
progress.Value = x;
This thrown an exception of course. 2.)
progress.BeginInvoke(new Action (delegate {
progress.Value = x;
}));Here the ProgressBar was updated after all calculation were finished. Tried to set a number of threads to 3 but it didn't change anything. The implemetation of the
Parallel
class is robust imho. Any help appreciated.Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
-
How to report a calculations' progress using a
ProgressBar
in a WinForms multi-threaded application? I want to utilize CPUs as much as I can and so use all (four) cores. After some googling the only thing I found is a suggestion to use aBackgroudWorker
, but it supports only one thread for the same work. The code looks like this:Parallel.For(0, width, delegate(int x)
{
//for (int x = 0; x < width; x++)
//{
//calculations with two more nested loops.
(***)
//}
});At (***) I tried the following: 1.)
progress.Value = x;
This thrown an exception of course. 2.)
progress.BeginInvoke(new Action (delegate {
progress.Value = x;
}));Here the ProgressBar was updated after all calculation were finished. Tried to set a number of threads to 3 but it didn't change anything. The implemetation of the
Parallel
class is robust imho. Any help appreciated.Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
You're making it harder than it has to be. Use a BackgroundWorker object to perform the calculation, and call the UpdateProgress method at the desired interval.
private BackgroundWorker myWorker = BackgroundWorker();
//--------------------------------------------------------------------------------
/// /// Initializes the background worker thread.
///
private void InitEncryptWorker()
{
myWorker.WorkerReportsProgress = true;
myWorker.WorkerSupportsCancellation = true;
myWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(myWorker_DoWork);
myWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(myWorker_RunWorkerCompleted);
myWorker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(myWorker_ProgressChanged);
}//--------------------------------------------------------------------------------
public void myWorker_DoWork(Object sender, DoWorkEventArgs e)
{
BackgroundWorker thisWorker = sender as BackgroundWorker;int progress = 0; for (int i = 0; i < 1000; i++) { if (!thisWork.CancellationPending) { // do some work here // and then report progress thisWorker.ReportProgress(progress); } }
}
//--------------------------------------------------------------------------------
private void myWorker_ProgressChanged(object sender,ProgressChangedEventArgs e)
{
if (e.ProgressPercentage != this.progressBar.Value)
{
this.progressBar.Value = e.ProgressPercentage;
}
}//--------------------------------------------------------------------------------
private void myWorker_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
{
}"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
You're making it harder than it has to be. Use a BackgroundWorker object to perform the calculation, and call the UpdateProgress method at the desired interval.
private BackgroundWorker myWorker = BackgroundWorker();
//--------------------------------------------------------------------------------
/// /// Initializes the background worker thread.
///
private void InitEncryptWorker()
{
myWorker.WorkerReportsProgress = true;
myWorker.WorkerSupportsCancellation = true;
myWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(myWorker_DoWork);
myWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(myWorker_RunWorkerCompleted);
myWorker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(myWorker_ProgressChanged);
}//--------------------------------------------------------------------------------
public void myWorker_DoWork(Object sender, DoWorkEventArgs e)
{
BackgroundWorker thisWorker = sender as BackgroundWorker;int progress = 0; for (int i = 0; i < 1000; i++) { if (!thisWork.CancellationPending) { // do some work here // and then report progress thisWorker.ReportProgress(progress); } }
}
//--------------------------------------------------------------------------------
private void myWorker_ProgressChanged(object sender,ProgressChangedEventArgs e)
{
if (e.ProgressPercentage != this.progressBar.Value)
{
this.progressBar.Value = e.ProgressPercentage;
}
}//--------------------------------------------------------------------------------
private void myWorker_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
{
}"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001This way you use only one thread for calculations and utilize 25% of quad core processors
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
-
gajatko wrote:
progress.BeginInvoke
Do not use BeginInvoke, as that will queue the invocation. Invoke should be sufficient.
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 alpha 4a out now (29 May 2008)Application hangs when using
Invoke
instead ofBeginInvoke
. From debugger, it stops on call to theInvoke
method.Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
-
How to report a calculations' progress using a
ProgressBar
in a WinForms multi-threaded application? I want to utilize CPUs as much as I can and so use all (four) cores. After some googling the only thing I found is a suggestion to use aBackgroudWorker
, but it supports only one thread for the same work. The code looks like this:Parallel.For(0, width, delegate(int x)
{
//for (int x = 0; x < width; x++)
//{
//calculations with two more nested loops.
(***)
//}
});At (***) I tried the following: 1.)
progress.Value = x;
This thrown an exception of course. 2.)
progress.BeginInvoke(new Action (delegate {
progress.Value = x;
}));Here the ProgressBar was updated after all calculation were finished. Tried to set a number of threads to 3 but it didn't change anything. The implemetation of the
Parallel
class is robust imho. Any help appreciated.Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
Parallel.For does not run the calculation in the background. It just splits the work to multiple threads, but the UI is blocked until the calculation has finished. If you want multiple threads to calculate in the background, use BackgroundWorker+Parallel.For.
-
Parallel.For does not run the calculation in the background. It just splits the work to multiple threads, but the UI is blocked until the calculation has finished. If you want multiple threads to calculate in the background, use BackgroundWorker+Parallel.For.
Frankly speaking I use my own implementation of Parallel to preserve .Net 2.0 compatibility but it works as you said, too. I will check if it works and post soon.
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
-
This way you use only one thread for calculations and utilize 25% of quad core processors
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
C'mon, be a programmer. If your calculations take so long that it's bothersome, put some
Thread.Sleep(10)
calls in the loop somewhere."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
C'mon, be a programmer. If your calculations take so long that it's bothersome, put some
Thread.Sleep(10)
calls in the loop somewhere."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001:-D
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
C'mon, be a programmer. If your calculations take so long that it's bothersome, put some
Thread.Sleep(10)
calls in the loop somewhere."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001John Simmons / outlaw programmer wrote:
C'mon, be a programmer
Yeach and then post your code on a Coding Horrors forum before someone else does. :laugh:
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
-
Parallel.For does not run the calculation in the background. It just splits the work to multiple threads, but the UI is blocked until the calculation has finished. If you want multiple threads to calculate in the background, use BackgroundWorker+Parallel.For.
BackgroundWorker+Parallel.For combination doesn't work.
progress.Value = e.ProgressPercentage;
throws an exception (Cross-thread operation not valid: Control 'progress' blah blah blah) and
progress.Invoke(new Action(delegate {
progress.Value = e.ProgressPercentage;
}));hangs the program. Any ideas? I though that this situation is so common that there is at least one satisfactory solution for it... :(
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
-
BackgroundWorker+Parallel.For combination doesn't work.
progress.Value = e.ProgressPercentage;
throws an exception (Cross-thread operation not valid: Control 'progress' blah blah blah) and
progress.Invoke(new Action(delegate {
progress.Value = e.ProgressPercentage;
}));hangs the program. Any ideas? I though that this situation is so common that there is at least one satisfactory solution for it... :(
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
Why is it hanging? Take a look at what the threads are doing. Why is the main thread blocked? What is it waiting for? Maybe you have a dead lock? (progress.Invoke waits for the main thread to finish any active event handlers, maybe the main thread is somehow still waiting for your calculation?)
-
John Simmons / outlaw programmer wrote:
C'mon, be a programmer
Yeach and then post your code on a Coding Horrors forum before someone else does. :laugh:
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
Yeah, I'll do that. In the meantime, you'll still be dickin' around with your code, and I will have finished this particular task moved on to something else. On the other hand, maybe using 25% of the CPU ain't so bad, after all. Of course, we can't determine the trade-offs because you haven't given us all the requirements.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001