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
New Developer
Posts: 23
Registered: ‎09-16-2008
My Device: Not Specified

How to create new thread

Hello all,

 

I am in a looped aplication with a main screen.  Then I need to make a remote call, and have the UI wait on a response.  Though the call execution responds immediately (ie True/False), I need to continue to make the UI wait on a callback function.

 

All of my code is in the main screen definition, which sets up the application, paints, and executes logic...Then I suppose the control goes back to my main's app.enterEventDispatcher, which waits on a UI Listener for the trackball.

 

At some point I would like this UI listener (which is in the main thread -----waiting for a choice field) to block and wait until remote code has called my callback function.  Below is an example... I would like to know the best way of accomplishing my goal.  

 

Do I, at this point create a new screen and push a newscreen onto the UI Application stack?  Then I suppose I could make the new screen output a message, and only listen on the callback functions from the remote server.  I would rather not do this because i'm not sure how to do inter screen communication.

 

Either way please let me know with a code snippet how to create a thread, and make the current executing thread block on a call back method.   Or if there is a better way to do it using my below example that would be great!

 

 

So here is an example:

public class App extends UIApplication

{

   main {

    App newApp = new App();

    newApp.enterEventDispatcher();

   }

 

   App()

   {

    pushScreen(new HelloScreen());

   }

 

 

}

 

Class HelloScreen Extends MainScreen

{

 

 HelloScreen()

 {

  stuff to output on screen

  choiceField = new ObjectChoiceField("....", choicestrs, 0)

   {

            //listen for the trackwheel on the choiceField

            protected boolean trackwheelClick(int status, int time)
            {

                     //code to do stuff

                     //While loop, that waits on the choiceField trackwheelClick until application is closed

                         {

                          // time to call remote server 

                         // block the current thread on a callback function

                         }

            }

 

 

  } ;


  add(choiceField); 
  choiceField.setFocus();

}

 

Thanks,

 

Chirag

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to create new thread

I think the model you have of the processing is slightly incorrect, but it might be just terminology.  Typically applications are not in a loop.  They are in an inactive state, waiting for an event.  Then they process the event, which could be user interaction (click on button, menu selection etc) or system interaction that they are listening for (for example event notification, phone call arrival, etc) and return to an inactive state. 

 

If you want your application to block, you will have to implement this s blocking yourself, effectively ignoring all User interaction until your request is responded to.  If that is what you really want to do, then I would definitely popup a little screen saying 'Connecting, please wait' or something similar, that lets the user know that the screen he is looking at is not going to respond to his/her input.

 

I would suggest that you don't delay doing a new screen just because you don't know how to do inter screen communication!  You are going to have to do it at some stage so do it now to give your users a nicer experience of your application.

 

The httpFetch sample in the Developer Guide and the httpdemo sample, both use a Thread in a similar way to what you are trying to do.  I would review the example in the JDE because it is slightly simpler.

New Developer
Posts: 23
Registered: ‎09-16-2008
My Device: Not Specified

Re: How to create new thread

Hi Peter_Strange,

 

Thanks for the response.  Yeah I just went through my code and I was calling it in a weird way, and now I realised that my loop is just waiting for a certain number of clicks before it processes any user input, and not really in a loop other than the app.EventDispatcher, so when the focus is shifter to my choiceField and a click occurs, the control is transferred to my listener, once i'm done with the info, it goes back to the app.EventDispatcher's control.  Which means nothing after the app.EventDispatcher line is executred until the application is closed. right?

 

However, I don't think pushing a newscreen onto the mainscreen solves my problem ....

 

So i figure that every time the app.EventDispatcher gets a click, it goes to the implementation of the class which I can include right after the choiceFiled declaration....

 

Which is cool... so can't I just block in the trackwheelClick listener?


Here is the sequence:

- user does something locally

- time to call remote server...remote server called

- remote server responds to say success

- At this point I want the main app.EventDispatcher to only listen on a specific method lets say it's called serverResponded(object obj)

- Remote server calls the callback method serverResponded(obj)

- app.EventDispatcher( )...I want to make serverResponded a listener for the main app, and I don't want any other listeners to do anything, or have them blocked.

 

So in essence, I want the app.EventDispatcher to listener to listen on a method callback, and nothing else until I have processed serverResponded's call.

 

Do you still recommend a pushing a new screen?  


Here is how i implemented my choiceField listener:

 

choiceField = new ObjectChoiceField("Select a Column: ", choicestrs, 0)
        {         
            int clickCount = 1;
            protected boolean trackwheelClick(int status, int time)
            {
               
               // do some stuff when trackwheel is clicked

 

            }

 

       }

 

Thanks,

 

Chirag
New Developer
Posts: 23
Registered: ‎09-16-2008
My Device: Not Specified

Re: How to create new thread

I think the problem that I have is ... how do I make my application wait on a callback method?  Ie making the callback method an event that is recognized by the app.EventDispatcher ().

 

Thanks,

 

Chirag

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: How to create new thread

Re loop terminology,

 

http://en.wikipedia.org/wiki/Message_loop_in_Microsoft_Windows

 

After a variety of anti-GUI rants, in the phone context they make sense due to physical limitations of the device.

One option it to grey out or otherwise modify menu's during the constrained time or put up "modal dialog"

boxes with a progress bar and cancel button. These are great if you want make people go insane as you

get all kinds of wierd things such as "modal" dialogs that come up behind the ( now disabled ) main window

which you can't close until you get to the modal box....

 

In short, I would try to write the app and code in such a way as to minimze lock-out issues just for robustness.

Menu based input in this context makes sense compared to command line input but you have to recognize

the things that go wrong with gui's don't go away here. There is rarely a reason to lock out a lot of options and

maintaining responsiveness just for the close and switch options is important.

 

 

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to create new thread

chico95 - This 'loop' model you seem to have about how the BlackBerry dispatches your application not really correct and I don't think it is going to help you when you are writing your application.  Please review the post marchywka has provided, but that might not answer you questions or cause you to review your model, so here is some more direct input:

 

I'm going to go through your post and expand on some of the points:

 

"not really in a loop other than the app.EventDispatcher" - not sure what you mean by the EventDispatcher - if you mean the Operating System which is processing events and passing control to the appropriate listeners, this is correct.  Remember that the Operating System is multi-threaded, so two things can be happening at once, for example an email could be arriving (driving a folder listener) while the user is typing on the keyboard , driving a key listener.

 

"Which means nothing after the app.EventDispatcher line is executed until the application is closed. right".  I presume you mean the call to enterEventDispatcher.  In fact, nothing after that line will be executed, period.  This line is telling the OS that the application is ready to run, and so the Application should put in the list of runnable applications.  After this is done, it is the Application's listeners that get driven.  The application is NOT a Thread, all the listeners are driven from Operating System Threads. 

 

"I don't think pushing a new screen onto the mainscreen solves my problem " - no it doesn't, it is just a visual indication to the user, that your Application is not going to process anything until it has a response from some action.  Whether doing this is a good idea or not is debatable.  As marchywka says, there is a good case for still allowing some interaction - so that the user can close the application if it takes too long or switch to another application.  But you seemed insistent on blocking the user, and this seems a good way to tell them that is what you are doing.

 

"so can't I just block in the trackwheelClick listener?"  You can ignore trackwheel clicks, but should NOT block it.  Remember it is not your thread that is running, it is a OS Thread that is giving your application a 'heads up' because you have expressed an interest in being told about the trackwheel.  In fact, on the BlackBerry, all User Interface type processing is performed on one OS Thread, called the Event Thread.  This should never be blocked, other processes (for example, a phone call which wants to display the incoming call screen) must process on this Thread.

 

"At this point I want the main app.EventDispatcher to only listen on a specific method" - there is no such thing as the main app.EventDispatcher .  You have listeners, you need to choose when you process the events that you are told about and when to ignore them.  It is your choice. 

 

"app.EventDispatcher to listener to listen on a method callback" - what you actually want is something in your application to listen for this call back, then re-enable the listening processing that you have ignored.

 

"Do you still recommend a pushing a new screen? " - I recommend telling the user that your application is ignoring User interaction at present, because it is waiting for some external event.  A popup to this effect seems responsible.  But that raises the question raised earlier, and expressed very well in marchywka post, do you really want to block all interaction, or is some interaction still useful.

 

Hope this makes some sense and helps

New Developer
Posts: 23
Registered: ‎09-16-2008
My Device: Not Specified

Re: How to create new thread

Hi Peter_Strange,

 

Thanks for the clarifications.  You are right there isn't a loop... and the app does go into the enterEventDispatcher method...which then the OS sends events to, and my application has the choice to listen to and act on.

 

So basically if there are things that I want actioned, they need to be done through the running application.  I read some more about callback methods, and basically I realised that in order to do what I want, I will need to create a new thread and implement the callback method in this thread's runnable.  When the thread's runnable gets the call back, if anything is blocked, this thread can notify any other thread that I need to at this point.

 

I don't actually need to block I guess, if the application is in a certain state, I can just choose to ignore the OS events I don't want action until the application state changes.  The callback Method in the new runnable thread would then modify the state of the application to signal actioning the new events.


Let me know if I have it right now.

 

Also, you are correct about not trying to do shortcut inter-screen or thread communication, if my application needs it, I should just code it Smiley Happy

 

I'm sure i'll be back asking more newbie questions soon! Smiley Happy

 

Thanks,

 

Chirag

 

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to create new thread

Glad you managed to pick some meaning out of what I said!  From what you have written, it would appear you have got it!

 

Just one thing I'm not happy with

"I realized that in order to do what I want, I will need to create a new thread and implement the callback method in this thread's runnable."

 

I'm not sure you need another Thread to watch over a Thread, look at the following:

 

Imagine your processing Thread, the one you are waiting on, is going to "callback" the 'completed() method when its finished.  That object that provides this completed method could be the same application screen that started the process Thread.  Say your screen has started the processing thread like the following:

 

Thread processThread = new ....;

processThread.start();

_waiting = true; // Note this flag

 

In your listeners, you will code:

if ( _waiting ) {

    return true; // suppress this interaction

}

 

Then your screen could have a completed() method like the following:

public void completed() {

    _waiting = false;

}

 

By turning this flag off, suddenly your listeners will all start processing.

 

Again hope this makes sense!

New Developer
Posts: 23
Registered: ‎09-16-2008
My Device: Not Specified

Re: How to create new thread

Hey,

 

I'm not sure what you mean by creating a thread to watch over a thread.  Either way you're right I don't even need to create a new processor thread really.

 

Ie.  I can just have an object that is part of the main application.  When this objects callback( ) method is called by a remote server, I just do what i need to do in the object, and pass in any objects that need modifications to the main application, and then signal the other listeners to start listening again.

 

Things are definitely becoming clearer Smiley Happy

 

Thanks again,


Chirag

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to create new thread

You got it!

 

To confuse this though, consider that your Application itself is an object, so when you say

"I can just have an object that is part of the main application"

your object could be the main application!  Or the Screen that you are currently displaying.  Or a specially created Object that just does this.  Your requirements will determine the best choice!