Blocking thread until background tasks are completed

Put your problem here if it does not fit any of the other categories.

Blocking thread until background tasks are completed

Postby tiagobt » Sat Jan 14, 2012 9:25 pm

Hello,

I have an application which loads several resources in memory during startup. Considering this operation takes some time, I'd like to allow the user to make the first interactions with the UI while the startup operations are happening. However, I can't let the user make further interactions with the UI before the resources are completely loaded.

My idea is to structure the activity as follows:

Code: Select all
public class MyActivity extends Activity {

   private LoadThread loadThread;

   private class LoadThread extends Thread {
      Context context;
      public LoadThread(Context context) {
         this.context = context;
      }
      public void run() {
         StartupManager.run(context); // Loads main resources (takes some time)
      }
   }

   public void onCreate(Bundle savedInstanceState) {
      this.loadThread = new LoadThread(this);
      loadThread.run();
      // Loads a few basic resources
      // Prepares the UI for first interaction
   }

   public void onActivityResult(int requestCode, int resultCode, Intent data) {
      // The code in here can only run when all resources are loaded,
      // so I should join the LoadThread
      try {
         this.loadThread.join();
      } catch (InterruptedException e) {
         e.printStackTrace();
      }
      // Continue using all the resources
   }

}


The user can do a few things before the main resources are loaded, but the code inside onActivityResult requires all resources to be loaded. That's why I thought about joining the LoadThread.

However, my logs show that nothing happens before the LoadThread finishes. Not even the initial UI interactions that I wanted. In fact, the UI does not even appear before the LoadThread finishes. Do you guys know why this happens?

I also thought about using the AsynTask class. As expected, I can do the background tasks inside the method doInBackground. However, I don't know how to block the code inside onActivityResult (which requires all resources) until the method onPostExecute is called. Using Thread#sleep in the main thread is probably not a very good idea.

Any other ideas?

Thanks so much,
Tiago
tiagobt
Freshman
Freshman
 
Posts: 7
Joined: Thu Jan 12, 2012 3:53 am

Top

Re: Blocking thread until background tasks are completed

Postby Phyll » Sun Jan 15, 2012 1:13 am

Hi tiagobt,

Phyll again,

I thought I might suggest you could simply set a boolean false while your resources are loading and not allow things that shouldn't to proceed until everything is ready. I suppose you really don't want to just abort something that was done in another activity and comes back to the onactivityresult either. For that I think you could put those intents into a queue of some kind and let a timer refire them at intervals or when the flag was gone. Kind of ugly but maybe something like that.

Hope that helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Blocking thread until background tasks are completed

Postby tiagobt » Fri Jan 27, 2012 4:56 pm

Thanks for the suggestion, Phyll. However, I found what I was doing wrong. I should have called "loadThread.start()" instead of "loadThread.run()"... Sorry about that!

TIago
tiagobt
Freshman
Freshman
 
Posts: 7
Joined: Thu Jan 12, 2012 3:53 am

Re: Blocking thread until background tasks are completed

Postby Phyll » Fri Jan 27, 2012 6:37 pm

Hi tiagobt,

Looks like you're right. Sorry I didn't catch that for you.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 9 guests