views:

85

answers:

2

I have two threads running one id UI and other is worker thread. Worker thread continuously reads on port for some data from server, when appears I need to update my webview in UI thread. Worker thread again continues to read and never ends. Plz suggest how to accomplish this??

it may be likely as 'signals in C++' which causes a method in called thread to be invoked..!! I tried: 1)As running worker thread on UI (runOnUIThread)may degrade UI webview performance, and if I put the thread on sleep it may miss data appeared at port when sleeping..(i m not sure!) 2)using Handler, I hav to specify time before calling thread again n again which may cause to miss data if appeared on port as like sleep(). 3)Having a separate thread in same class,it gives: any other thread cant update view of main UI thread.

Plz help.. :(

A: 

Maybe I am missing something, but it seems like creating a Handler object and then using the post() method is what you need: http://developer.android.com/reference/android/os/Handler.html#post%28java.lang.Runnable%29

BenV
using Hadler object and than using post(), slows down the performance of UI thread very badly, as in runnable it hangs on reading data from server(in.readline). Same as with runOnUiThread.I also tried observer pattern in JAVA, but alas :(1. I have two threads 'worker' and 'UI' thread.2. worker keep on waiting for data from server, when gets it notifies to UI thread.3. On update UI Toast a msg on screen.Step 3 is problem as it says : Only the original thread that created a view hierarchy can touch its views.Thanks in advance.
Placidnick
A: 

I had a similar problem. I have a UI thread and a few threads that do backend serverices. I use the observer Pattern to have the services update the UI and to do so I have the update method in a new thread and call runOnUiThread() to have it update the UI. It seems to work well for something quick. If you want something better than a quick band-aid I would suggest reading this android threading guide from Google and trying to incorporate it with your design.

Mlove