Intense process - update progressbar

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Intense process - update progressbar

Postby madkins » Thu Mar 12, 2009 1:59 am

Hello.

I have a button when pressed, it executes for a long time and of course I get force close unless I create a thread. So that is what I did, I put that in a runOnUIThread(). While the process is running I would like to show the progress. I know how to add to the progress bar, but it really doesn't add until the main process is complete. Which is worthless...

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. OnClickListener goCalcBtnOnClick = new OnClickListener() {
  2.  
  3.         public void onClick(View view) {
  4.  
  5.  
  6.  
  7.         runOnUiThread(new Runnable() {
  8.  
  9.           Override
  10.  
  11.           public void run() {
  12.  
  13.               doResetProgress();   // Set progress to 0
  14.  
  15.               doMajorCalcs();      // For each record out of total records increment progress by 1
  16.  
  17.               doShowReport();      // clear the progress
  18.  
  19.           }
  20.  
  21.         });
  22.  
  23.  
  24.  
  25.         }
  26.  
  27. };
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


In doMajorCalcs(), I do some calcs, then update the progress bar. Like I said, it updates it, but not until my major calcs are completed and the report is showing.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. mHandler.post(new Runnable() {
  2.  
  3.                     public void run() {
  4.  
  5.                         prog.setProgress(mProgressStatus);
  6.  
  7.                     }
  8.  
  9. });
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Any ideas? Do I need to call the processes from the button click then put the code for the methods inside runonuithread? So each method would have it's own runonuithread?

Thank you.
madkins
madkins
Junior Developer
Junior Developer
 
Posts: 10
Joined: Fri Nov 21, 2008 1:09 pm

Top

Postby MrSnowflake » Thu Mar 12, 2009 8:23 pm

Uhm yeah, that's pretty normal :). You are trying to update the user interface, which is ok, BUT you are also doing the expensive calcs in the user interface thread, so your calcs occupies whole the ui thread and thus the ui can't be updated. You should run the calcs in a different thread and update the progress bar from there.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby madkins » Thu Mar 12, 2009 9:21 pm

Thank you. I started working on that last night, much nicer!!! I appreciate the response.
madkins
Junior Developer
Junior Developer
 
Posts: 10
Joined: Fri Nov 21, 2008 1:09 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 5 guests