weird android threading? [solved... well partially]

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

weird android threading? [solved... well partially]

Postby sandis84 » Wed Oct 14, 2009 4:24 pm

I find myself over and over again being greatly frustrated by how android threads. Look at this code, and inform me how this should be coded. What I am trying to do, is simply to show a progressdialog while a server-request is happening. I also want to avoid a "application not responding"-message, so the request is done in another thread. "lock" is a global semaphore.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ProgressDialog pd = ProgressDialog.show(this, "Working..", "Contacting server...", true, false);
  2.  
  3. lock = new Semaphore(0);
  4.  
  5. Thread thread = new Thread(this);
  6.  
  7. thread.start();
  8.  
  9. try {
  10.  
  11.         lock.acquire();
  12.  
  13. } catch (InterruptedException e) {
  14.  
  15.         e.printStackTrace();
  16.  
  17. }
  18.  
  19. pd.dismiss();
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


This works perfectly, with one exception: The progressdialog is not shown until after the lock is aquired. Since that is when the dialog is dismissed, it is never shown. How do I make the dialog show up on time? Any help or suggestions would be greatly appreciated. Thanks in advance.

//Per Sandström
Last edited by sandis84 on Wed Oct 14, 2009 6:53 pm, edited 1 time in total.
sandis84
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Fri Aug 07, 2009 3:54 pm

Top

Postby rdomloge » Wed Oct 14, 2009 4:55 pm

Sorry if I am missing something, but don't you need to call show() on the dialog?

Also, you would be better off not locking the thread with a semaphore - I suspect that this would cause Android to believe that the application was no longer responding, since the method could take a long time.

Perhaps you could get the thread code to dismiss the dialog when it is finished..?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. final ProgressDialog pd = ProgressDialog.show(this, "Working..", "Contacting server...", true, false);
  3.  
  4. pd.show();
  5.  
  6. Thread thread = new Thread(this);
  7.  
  8. thread.start();
  9.  
  10.  
  11.  
  12. public void run() {
  13.  
  14.     doThing();
  15.  
  16.     pd.dismiss();
  17.  
  18. }
  19.  
  20.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
rdomloge
Junior Developer
Junior Developer
 
Posts: 24
Joined: Sat Jan 24, 2009 2:11 pm

Postby sandis84 » Wed Oct 14, 2009 6:34 pm

Oops, forgot to write that. Yes, I do use .show() in my code, but it does not help. =)

you also seem to be correct that android interprets waiting for a lock as non-responsive. I guess I could use something like a handler instead. However, this still does not solve my problem with the non-showing dialog. :(
sandis84
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Fri Aug 07, 2009 3:54 pm

Postby sandis84 » Wed Oct 14, 2009 6:52 pm

wait a minute, I DO have a .show() in my code!

However, I have solved the problem. Using a handler solved it. I have a theory why this is. Androids threading sucks to the extreme, so when I wanna show a dialog, it actually dont do it until the current function has been completed. Maybe because of this silly "only one GUI-thread"-rule they have. Anyhow, with the usage of handler the function is finished and the dialog is shown. Nice.
Last edited by sandis84 on Wed Oct 14, 2009 6:53 pm, edited 1 time in total.
sandis84
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Fri Aug 07, 2009 3:54 pm

Postby rdomloge » Wed Oct 14, 2009 6:53 pm

My mistake.

Personally I'm quite familiar with the Java threading model and find the paradigm fairly simple to use. Perhaps it'll seem easier once it's second nature..?
Last edited by rdomloge on Wed Oct 14, 2009 6:59 pm, edited 1 time in total.
rdomloge
Junior Developer
Junior Developer
 
Posts: 24
Joined: Sat Jan 24, 2009 2:11 pm

Postby sandis84 » Wed Oct 14, 2009 6:57 pm

Pro tip to others reading this and suffering the wrath of the android gods: I just realized I forgot about the awesomeness of AsyncTask. It is the perfect class to use in threading-situations like this. I should have used it instead of a handler :P
sandis84
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Fri Aug 07, 2009 3:54 pm

Top

Postby sandis84 » Wed Oct 14, 2009 7:08 pm

rdomloge wrote:My mistake.

Personally I'm quite familiar with the Java threading model and find the paradigm fairly simple to use. Perhaps it'll seem easier once it's second nature..?


np, thanks for the help anyway =) Hopefully I will get used to how this works in due time. But I still dont feel that I understand the android implementation fully. Do you know how it works? Is it like every function of an activity-object has the synchronized-keyword? It seems like it, but I dont see the logic behind it. It would feel more intuitive if only the actual editing of the UI should be synchronized. But then again, maybe simply everything that can be even remotly blocking should be moved to another thread.
sandis84
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Fri Aug 07, 2009 3:54 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Alexa [Bot], Google [Bot] and 22 guests