C# Progress Bar
- Hi,I need to implement s progressbar in my application. However, I need to pass the progressbar.value variable to some evaluating function (which say loops 1000 times) so that the function can update the value of the progress bar as the loop gets executed. However, progressbar.value is a property and hence it cannot be passed as a reference to the evaluating function. How then can I update the value from within the function?Any help will be greatly appreciated.p.s. One method that I have thought of is to make a static function called update_value in the class that has the progress bar, which updates the value of the progressbar. Then while calling the function, the object can be passed as a reference (which has the static function) to the evaluating function and hence the value can be updated from the function by means of the static function. Is this a good way of achieving the objective?
Answers
- You can just invoke the progress bar wherever you need to inside your thread. The invoke process would go something like:
// Delegate function
void UpdateProgressSafe(int value)
{
this.progressBar1.Value = value;
}
// The delegate
delegate void UpdateProgressDelegate(int value);
// The delegate member
UpdateProgressDelegate UpdateProgress = new UpdateProgressDelegate(UpdateProgressSafe);
// Invoke wherever you need to in another thread
// First parameter is the delegate
// Second parameter is the value
progressBar1.Invoke(UpdateProgress, new object[] { 75 });
All Replies
You would be much better using a background worker to update the progress - this way it wont look like the application has crashed!
Take a peek around for a toutorial on background workers - if you cant find anything Ill write a detailed guide for you.
Ross
hi
man you can use the timer it would be a good solution
bye
You can't pass a property value to a function by reference if it's an integer data type. If you want to be able to change the progessbar.value in your function then, instead of passing the progressbar.value as a parameter in your function pass the whole progressbar to the function by reference and you should be able to change it's value from inside the funcion.
You should pass the progress bar as ByRef to the function so that it's not a copy of the control (as it would be if you passed it with Byval)
Hope this works!
Take a look at the sample http://www.ondotnet.com/pub/a/dotnet/2005/07/25/backgroundworker.html
Ravi Gubbi wrote: Take a look at the sample http://www.ondotnet.com/pub/a/dotnet/2005/07/25/backgroundworker.html
As I said before this is deffinatly the way you should do progress bar - I personally would not pass a referance to it, you will enter a world of hate in a multi threaded application.
Ross
- You can just invoke the progress bar wherever you need to inside your thread. The invoke process would go something like:
// Delegate function
void UpdateProgressSafe(int value)
{
this.progressBar1.Value = value;
}
// The delegate
delegate void UpdateProgressDelegate(int value);
// The delegate member
UpdateProgressDelegate UpdateProgress = new UpdateProgressDelegate(UpdateProgressSafe);
// Invoke wherever you need to in another thread
// First parameter is the delegate
// Second parameter is the value
progressBar1.Invoke(UpdateProgress, new object[] { 75 }); Take a look at my blog posting on background workers - http://blog.the-dargans.co.uk/search/label/Background%20Worker
It is a lot neater than invoking an update, and a lot safer than passing a referance to a ui thread object.
Thanks
Ross
Tried this. Not working. My progress bar still freezes to death !
Going to try the worker process solution...


