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: 179
Registered: ‎09-11-2009
My Device: Not Specified

Re: AnimatedGIF popup: Appears When PushScreen Only, But Not When PushScreen, then PopScreen When Do

OK.

 

a) I put the if() statement in the code I thought was running off the event thread, and it displayed the message.

 

b) This statement, the invokeLater(animatedScreen), worked.

 

Is this a bad thing?  Does this mean that I'm still blocking the event thread?  Since it works, should I leave the code this way or do I need to rewrite somewhere?

 

 

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

Re: AnimatedGIF popup: Appears When PushScreen Only, But Not When PushScreen, then PopScreen When Do

"Does this mean that I'm still blocking the event thread?  "

 

Yes

 

"Is this a bad thing?"

 

I think so.  But I think you said you were writing this just for you, and it will only impact the Browser, so its up to you. 

 

The worry, at least from my perspective, is that you think you have done enough to get off the Event Thread and your code is still there.  So you need to review your code and figure out why this is happening, because the current situation indicates that you don't understand something or have coded something wrong.  So I would try to figure this out now, so you don't make the same mistake in the future..

Developer
Posts: 179
Registered: ‎09-11-2009
My Device: Not Specified

Re: AnimatedGIF popup: Appears When PushScreen Only, But Not When PushScreen, then PopScreen When Do

 


peter_strange wrote:

"Does this mean that I'm still blocking the event thread?  "

 

Yes

 

"Is this a bad thing?"

 

I think so.  But I think you said you were writing this just for you, and it will only impact the Browser, so its up to you. 

 

The worry, at least from my perspective, is that you think you have done enough to get off the Event Thread and your code is still there.  So you need to review your code and figure out why this is happening, because the current situation indicates that you don't understand something or have coded something wrong.  So I would try to figure this out now, so you don't make the same mistake in the future..


 

 

I'm a little confused as to why the code on the event thread is a bad thing.  Don't I have to get on the event thread to push Ui Screens, since I started another thread to do the networking stuff?

 

 

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

Re: AnimatedGIF popup: Appears When PushScreen Only, But Not When PushScreen, then PopScreen When Do

Confusion reigns. I thought you had added the test to your networking Thread. 

Developer
Posts: 179
Registered: ‎09-11-2009
My Device: Not Specified

Re: AnimatedGIF popup: Appears When PushScreen Only, But Not When PushScreen, then PopScreen When Do

I did.  But, since I have no real GUI interface, I had to make another thread to handle the credentialsScreen dialog.

 

Here's a diagram of how the application looks.  Hopefully this will explain things:

 

1. ApplicationMenuItem.run()

     Thread t = new Thread(appThread)

      t.start()

 

2. appThread

    networkThread nt = new networkThread

    public void run()

    invokeLater(new Runnable)

    do

          c = nt.packageCreds               <-----------this networkThread method pops the credentials dialog & packages creds

          nt.auth(c)                                    <-----------this networkThread method verifies the creds & has Dialog.alert(success)

    while rc !=200                                  <-----------loop is to redisplay creds dialog if creds are wrong

    invokeLater(newRunnable)

    nt.add(c, string url)                          <----------this network method uses creds in static field after they're verified  & url

                                                                                also has Dialog.alert(success) or Dialog.alert(fail)

 

3. networkThread

     StringBuffer packageCreds()        <----------method prompts for creds, returns stringBuffer of creds&apikey

                                                                                  creds prompt is custom dialog

     int auth(StringBuffer c)            <--------method w/creds as arg, auths against remote URI & returns int return code

                                                                       animatedScreen displayed during work

     int add(StringBuffer c, String u)  <--------method w/creds, url as arg, adds url to remote URI & returns int return code

                                                                            animatedScreen displayed during work.

 

 

Does this help?

 

 

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

Re: AnimatedGIF popup: Appears When PushScreen Only, But Not When PushScreen, then PopScreen When Do

Here is an alternative design.  it is based around the netowrk thread having to ask the user for credentials, then sending these off to the Server to check them.  Once the credentials have been checked and are OK, then some following network processing can be done.  I'm not sure that is what you want to do.

 

You will notice what happens here is that the network Thread runs off the Event Thread and pops onto the Event Thread when something needs to be done that requires user interaction.

 

1. ApplicationMenuItem.run()

   Thread t = new Thread(networkThread)

    t.start()

 

2. Network Thread

// Off event Thread

  create 'animationScreen'

  create 'getCredentialsScreen'

  do

    invokeandWaitr(new Runnable) {

// Now in Event Thread

      pushModalScreen(getCredentialsScreen)

             (or Dialog.ask)

// will return when user has filled in details

      push animation screen

    }

// Not on event thread 

    extract credentials from screen

    packageCreds

    authenticate

    if ( rc != 200 )

      invokeandWaitr(new Runnable) {

        dismiss animation Screen

      }

    }

  while rc != 200    

// Now not on Event Thread, have credentials and

// have an animation screen being displayed

  add process

// whatever that is, not clear on this, don't need to know

  invokelaterr(new Runnable) {

// Now on event thread 

// Won't run immediately and the network Thread will continue

    dismiss animation Screen

    display success or failure Dialog

  }

     

Some comments on your current implementation

//-----------------------------------------

 

1. ApplicationMenuItem.run()

     Thread t = new Thread(appThread)

      t.start()

 

// This is good, not running Event Thread.

 

2. appThread

    networkThread nt = new networkThread

// does not start network Thread.

    public void run()

    invokeLater(new Runnable)

// Now back on Event Thread!  You should only have UI updating stuff on Event Thread

    do

// So not invoking the network processing as a Thread (using .start), invoking method call directly,

          c = nt.packageCreds               <-----------this networkThread method pops the credentials dialog & packages creds

          nt.auth(c)                                    <-----------this networkThread method verifies the creds & has Dialog.alert(success)

    while rc !=200                                  <-----------loop is to redisplay creds dialog if creds are wrong

    invokeLater(newRunnable)

// On Event Thred again

    nt.add(c, string url)                          <----------this network method uses creds in static field after they're verified  & url

                                                                                also has Dialog.alert(success) or Dialog.alert(fail)

 

3. networkThread

     StringBuffer packageCreds()        <----------method prompts for creds, returns stringBuffer of creds&apikey

                                                                                  creds prompt is custom dialog

     int auth(StringBuffer c)            <--------method w/creds as arg, auths against remote URI & returns int return code

                                                                       animatedScreen displayed during work

     int add(StringBuffer c, String u)  <--------method w/creds, url as arg, adds url to remote URI & returns int return code

                                                                            animatedScreen displayed during work.

//-----------------------------------------

 

Developer
Posts: 179
Registered: ‎09-11-2009
My Device: Not Specified

Re: AnimatedGIF popup: Appears When PushScreen Only, But Not When PushScreen, then PopScreen When Do

I'll try your design.  I originally went with the design I outlined because in a different post you mentioned not putting custom screens in the networking class...since I'd probably re-use it.  I guess I can always copy it off to another folder before rewriting it.

 

Also, and I should've mentioned it in the post, I left out quite a bit of code in that diagram...I wanted to give a rough idea of what was going on.  For example, I know networkThread nt = new networkThread() doesn't start the network thread but it does initialize the object used in the appThread.

 

Your explanation helps...for some reason I thought I had to wrap all the code that included the UI in an invokeLater...not just when I needed to pop a UI screen.

 

Thanks again...I'll post back with an update.

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

Re: AnimatedGIF popup: Appears When PushScreen Only, But Not When PushScreen, then PopScreen When Do

"mentioned not putting custom screens in the networking "

 

Yes this is not the Observer pattern that I originally suggested you to implement.  This is easier, but not transferable. 

 

To convert this to the Observer pattern, you would have a class create the network Thread, register itself as an Observer, and the network Thread would issue callbacks to that class - the class would then handle the UI interaction.  But what is important at this stage I think is to get the principles. 

 

"I should've mentioned it in the post, I left out quite a bit of code in that diagram"

 

I had expected that there was more, and in fact there more you need to think about to such as error recovery and giving the user the ability to cancel the request.  Again the principle is more important at this stage I think, so a simple explanation was the right way to go. 

 

I'm sorry that you have gone off potentially the wrong way, I feel like I have caused you extra work, I hope it all comes right in the end.

Developer
Posts: 179
Registered: ‎09-11-2009
My Device: Not Specified

Re: AnimatedGIF popup: Appears When PushScreen Only, But Not When PushScreen, then PopScreen When Do

 


peter_strange wrote:

 

I'm sorry that you have gone off potentially the wrong way, I feel like I have caused you extra work, I hope it all comes right in the end.


No apology needed...I've learned quite a bit while writing and re-writing this app that I wouldn't have learned otherwise.  This is a good thing.
What I think I'll do is save the network, creds Dialog and animated wait classes off to another directory before making the changes.  Then, when I have a chance, I can bring them back and work on the Observer pattern for the network class...see if I can get it working that wa.
Unfortunately that will have to be after my exam tomorrow, work, etc...Smiley Sad

 

 

Developer
Posts: 207
Registered: ‎07-31-2009
My Device: Not Specified

Re: AnimatedGIF popup: Appears When PushScreen Only, But Not When PushScreen, then PopScreen When Do

Peter :

 

 

I have a problem with the gif field. What i am trying to do is show a progress screen when there is some network activity. I ve put all those network work in to a different thread using invokelater() and in the main event thread i am displaying the gif image. Inside the runnable after the network work is done am closing the image. All this works fine. The image comes up during the network operation and when its done it closes. The problem is , its no longer an animation. Its as though it just renders the first frame of the gif image . I initially thought it was cos the network activity was pretty fast but it was the same even after i put a Thread.Sleep() in the runnable. 

 

I have already checked if the image was converted by the compiler. It has not .

 

Any help would be great.. 

 

------------------------------------------------------------------------------------------------------------------------
Click the Kudos! badge on left side of the message , if you are happy with a solution given by a user. When you do so, you are saying thanks to its author.
Mark a reply as a solution , if you think your question has been answered.
To mark a message as a solution: Click "Accept as solution" icon on the reply.