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
Posts: 34
Registered: ‎09-15-2009
My Device: Not Specified
Accepted Solution

Dialog.alert() and Threads seem to collide.

Whenever I run a new thread from the main program, it works just fine as long as I just change some figures, or output to sysout. Whenevery i use a Dialog.alert from a thread, I get a RuntimeException...

 

I tried passing the main class as an argument to the thread, then from the thread run a custom mainclass.displayPopup(); function that popups the but it still fails with a runtimeException Smiley Sad

 

Don't I understand the usage of Threads? I want  a Dialog.alert() to apear 3 seconds after the screen from the moment the screen that holds the main code is pushed...

Developer
Posts: 16,932
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Dialog.alert() and Threads seem to collide.

if you access the UI from outside the main event thread, for example from a separate thread, you have to use invokelater or synchronize on the eventlock.

----------------------------------------------------------
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
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Dialog.alert() and Threads seem to collide.

Developer
Posts: 34
Registered: ‎09-15-2009
My Device: Not Specified

Re: Dialog.alert() and Threads seem to collide.

[ Edited ]

Thanks for the help so far. I'v added an invokeLater(new Runnable) within the thread, to push the drawing action to the eventThread. It seems however that this action is blocking, my screen is unresponsive for a number of seconds before the popup is displayed...

 

I understand now that there is an event queue that the eventThread handles, in order to ensure only 1 thing is drawn on the screen at a time. You can either push something on the todo stack for the eventThread by using invokeLater, or get the eventThread yourself (which is blocking so this behaviour i am experiencing could be expected).

 

I am using the (non blocking?) invokeLater from within my thread:

 

 

import net.rim.device.api.system.Application;

public class QuestionDispatcher extends Thread {

  GameScreen gs;
  
  public QuestionDispatcher(GameScreen gs) {
    this.gs = gs;
  }
  
  public void run() {
    try {
      sleep(3000);
    } catch (Exception e) {}
    Application.getApplication().invokeLater( new Runnable() {
      public void run() {
        gs.displayQuestion();        
      }
    });
  }

}

so why is the screen hanging when I do

 

 

this.qdist = new QuestionDispatcher(this);
this.qdist.run();

 from the GameScreen?...

 

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

Re: Dialog.alert() and Threads seem to collide.

To start a separate Thread you use start:

this.qdist.start();

 

If you use

this.qdist.run();

then you are just calling the run method on your own Thread.

Highlighted
Developer
Posts: 34
Registered: ‎09-15-2009
My Device: Not Specified

Re: Dialog.alert() and Threads seem to collide.

Aargh too stupid :X thanks mate, lets see here Smiley Happy