Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
KeniF
Posts: 131
Registered: ‎08-13-2008
My Device: Not Specified

Background thread to update GUI

[ Edited ]

My application has a background thread doing stuff and when the user opens the GUI (2 entry points), it needs to display the current state of the thread. I've been looking at the following:

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800783/How_To_-_Allow_a_background_listener_to_detect_and_update_a_GUI_application.html?nodeid=827026&vernum=0

 

but there are a few things that I don't understand.

 

(1) What is the following long value?

 

public static final long RTSID_MY_APP = 0x7451402f595f81a5L;

(2) Is this even a good example for what I'm doing?

I only need to obtain a state from the thread (int).

Message Edited by KeniF on 08-15-2008 11:41 AM
Please use plain text.
Developer
simon_hain
Posts: 16,215
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Background thread to update GUI

RTSID_MY_APP is a key used for the runtime store. the listener application puts a reference to the GUI app into the runtime store to ensure that it is running only once (singleton). if there is already a reference it uses it instead of creating a new one.

based on the information you gave i think it would be better to implement a two way communication using global events (for example). or do you want to have the status of the thread updated all the time while your GUI is running?
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
KeniF
Posts: 131
Registered: ‎08-13-2008
My Device: Not Specified

Re: Background thread to update GUI

How does one generate a RTSID then?

 

Actually the GUI already has a reference to the thread. However, since I only do the following when the GUI is closed, the GUI is actually still running. Anyway, how can I get the thread to continuously update the textField when the GUI is active?

public void close(){ Application.getApplication().requestBackground(); //hides screen }// close

 

Please use plain text.
Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: Background thread to update GUI

Just to prove if you get two developers you will get two different ways of doing it, I would put the background thread into RuntimeStore with a GUID known by the UiApplication.  When the UiApplication starts, it can find the background thread, and call any method it likes on it (e.g. getStatus()).  Note that you should also use isAlive() on the Thread first to make sure it is actually still running!

Please use plain text.
Developer
KeniF
Posts: 131
Registered: ‎08-13-2008
My Device: Not Specified

Re: Background thread to update GUI

[ Edited ]

That will only work once because I only set it to background on close. The GUI cannot be restarted!

 

The GUI already has a reference to the thread, I just need to continuously update the state!

 

:smileyfrustrated: I think I can solve it by having the thread take a reference of the GUI...

Message Edited by KeniF on 08-15-2008 12:33 PM
Please use plain text.
Developer
KeniF
Posts: 131
Registered: ‎08-13-2008
My Device: Not Specified

Help!!

Ok, I tried having the background thread take a reference to the GUI, but it won't work because there are 2 entry points - either 2 instances of threads will be created (the code below), or the GUI is not linked to the thread (when I move new bgThread and bgThread.start() to within the else statement.

 

Please help !! 

 

final class Sync extends UiApplication{ private static long imsiLong; private static MyGUI myGUI; private static BackgroundThread bgThread; // 2 entry points, defined in 2 projects public static void main(String[]args){ imsiLong=findIMSI(); myGUI=new MyGUI(imsiLong); bgThread=new BackgroundThread(myGUI); bgThread.start(); // Load the GUI (entry point 1) if(args!=null&&args.length>0&&args[0].equals("gui")){ new Sync().enterEventDispatcher(); }// GUI else{ // Blackberry Autostart (entry point 2) }// autostart }//main

 


 

Please use plain text.
Developer
simon_hain
Posts: 16,215
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Help!!

http://en.wikipedia.org/wiki/Singleton_pattern
http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
KeniF
Posts: 131
Registered: ‎08-13-2008
My Device: Not Specified

Re: Help!!

I've tried using singletons in my program, but still 2 instances were created. I think this is because with 2 entry points, the application is created in 2 processes. I think I'll use the idea from the link in my first post. Thanks anyway.
Please use plain text.
Developer
simon_hain
Posts: 16,215
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Help!!

can't you define an entry point to create the background thread, i.e. an autostart entry point?
your point is valid though, a singleton would not work because the different entry points do not share the instance.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
KeniF
Posts: 131
Registered: ‎08-13-2008
My Device: Not Specified

Re: Help!!

[ Edited ]

Yeah, but I wanted the background thread to update the GUI so it takes an instance of the GUI. The background thread has to be created before the if statement for the GUI, or the link will not be established (if the thread is created/started in the else statement)

 

Edit: Fixed! Basically, RuntimeStore is used to check if an instance of background thread exists.

 

else{ //autostart bgThread=BackgroundThread.waitForSingleton(); bgThread.start(); }// autostart public static BackgroundThread waitForSingleton(){ RuntimeStore rtStore=RuntimeStore.getRuntimeStore(); Object obj=rtStore.get(RTSID); if(obj==null){ rtStore.put(RTSID,new BackgroundThread()); return (BackgroundThread) rtStore.get(RTSID); }// if not created else{ //already created return (BackgroundThread) obj; }//else }// waitForSingleton

Message Edited by KeniF on 08-18-2008 08:36 AM
Message Edited by KeniF on 08-18-2008 11:29 AM
Please use plain text.