Ask a questionAsk a question
 

AnswerC# Progress Bar

  • Thursday, June 28, 2007 4:32 AMSameerSCT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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

  • Thursday, June 28, 2007 10:19 PMÐãvę Âņđęŕŝőŋ1 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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

  • Thursday, June 28, 2007 7:01 AMRoss Dargan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Thursday, June 28, 2007 7:27 AMali Naqvi Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    hi

     

    man you can use the timer it would be a good solution

     

    bye

  • Thursday, June 28, 2007 6:43 PMWestor Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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!

     

  • Thursday, June 28, 2007 7:06 PMRavi Gubbi Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
  • Thursday, June 28, 2007 8:02 PMRoss Dargan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
     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

  • Thursday, June 28, 2007 10:19 PMÐãvę Âņđęŕŝőŋ1 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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 });
  • Saturday, June 30, 2007 8:29 AMRoss Dargan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Friday, February 22, 2008 4:23 PMMarc Roussel Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    Tried this. Not working.  My progress bar still freezes to death !

     

    Going to try the worker process solution...