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
Highlighted
Developer
Posts: 128
Registered: ‎11-05-2009
My Device: 9700
My Carrier: 9700
Accepted Solution

Dialog not closed after calling thread.sleep

Hi

  I am displaying dialog and then call thread.sleep(5000), after that i am calling two more dialog alerts. The problem is when it displaying first dialog onclick of ok button its not getting closed and its open second dialog automatically. do u have any idea.

 

Sample code

 

try {

       Dialog.alert("hi");

 

       // business logic

}

catch

{

   Thread.sleep(3000);

   Dialog.alert("hello");

}

 

After display "Hi" i am not able to close this dialog. I tried to put the Thread.sleep inside the invlodLater() mtd, no luck

 

Any idea?

 

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

Re: Dialog not closed after calling thread.sleep

The problem here is that you are running on the Event Thread, and as a result, you are blocking the Ui when you sleep, so the Ui is not being updated.

 

Following is an invocation that does not have this problem.  I suspect that it does not do what you want, but hopefully you wiill get the idea from here.

 

Code not compiled, I hope you can figure out an issues.

 

Thread t = new Thread() {
 public void run() {
  UiApplication.getUiApplication().invokeAndWait(new Runnable() {
   public void run() {
    Dialog.alert("First dialog");
   }
  });
  try {
   Thread.sleep(5000);
  } catch (Exception e) {
  }
  UiApplication.getUiApplication().invokeAndWait(new Runnable() {
   public void run() {
    Dialog.alert("Second dialog");
   }
  });
 }
};
t.start();

 

Developer
Posts: 128
Registered: ‎11-05-2009
My Device: 9700
My Carrier: 9700

Re: Dialog not closed after calling thread.sleep

Thanks for reply. Is it possible to do without creating Thread

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

Re: Dialog not closed after calling thread.sleep

Not sure what you mean by that.

 

If you want to code a Thread.sleep(), or code a similar blocking wait, you have to be off the Event Thread.  And you can't do that without starting another Thread.

 

Perhaps this will help:

http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865