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: 10
Registered: ‎07-17-2008
My Device: Not Specified

updating UI from system thread (ui engine accessed without holding the event lock)

Hi there,

I hope you can help me out.

 

I am registering for a Phone.addPhoneListener and try to update a richtextfield whenever i receive the udpate ( e.g. phone call received).

 

my update function my PhoneListener calls looks like this:

 

public void update()
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
mLogField.setText(dc.mLogText.toString());
}
});
}

 

yet I am getting a uncaught exception:UI engine accessed without holding the event lock"

 

so i am wondering how i can properly update the richtextfield.

 

thank you,

 

blizzz


 

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: updating UI from system thread (ui engine accessed without holding the event lock)

Has the application already entered the event dispatcher when this listener event occurs?

 

See Application.enterEventDispatcher().

 

 

New Developer
Posts: 10
Registered: ‎07-17-2008
My Device: Not Specified

Re: updating UI from system thread (ui engine accessed without holding the event lock)

I think so. my app is a normal uiapplication calling theApp.enterEventDispatcher(); in the beginning. i extend MainScreen and in the constructor i register for the phone listener and my ui builds app and the richtext field is displayed. if i then simulate a phone calli get the exception as noticed before.

 

 

*/ public static void main(String[] args) { Demo theApp = new Demo(); theApp.enterEventDispatcher(); } /** * Constructor. */ private Demo() { DemoScreen = new DemoScreen (); pushScreen(DemoScreen ); }

 

 

 

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: updating UI from system thread (ui engine accessed without holding the event lock)

That error usually indicates that you are attempting to update a UI component from outside of the event thread.

 

Where is the listener being set up? Have you set up an altermate entry point for the application to do this?

 

-de

 

New Developer
Posts: 10
Registered: ‎07-17-2008
My Device: Not Specified

Re: updating UI from system thread (ui engine accessed without holding the event lock)

Hi, thanks for your reply.

 

the listener is setup in my screen which extends from mainscreen. I don't use any alternate entry points.

 

DemoScreen()
{
super(DEFAULT_MENU | DEFAULT_CLOSE);

mLogField = new RichTextField();

Phone.addPhoneListener(mPhoneLogger);



}


class PhoneLogger implements PhoneListener{


}

 

 

 

 

Maybe i should try to compile a complete sample where i just instead of snippets, i will work on that, likely i miss something completely. as far as i understand when the phone app calls the phonelisteners call back it is a different thread, maybe that is the problem?

 

New Developer
Posts: 10
Registered: ‎07-17-2008
My Device: Not Specified

Re: updating UI from system thread (ui engine accessed without holding the event lock)

here how i resolved it for now,

 

UiApplication.getUiApplication().invokeLater(new Runnable()

 i changed that to

 

 

mUiApp.invokeLater(new Runnable()

 in mUiApp i store a reference to my ui app which created my mainscreen. Before UiApplication.getUiApplication() would get the ui application instance  of the Phone app which was invoking my PhoneListener callback, so my invokeLater would end up on the Phone apps eventDispatch thread instead of my apps one,  and i guess that's not cool. At least that's how I imagine what's going on :-)

 

 thank you

 

 

 

 

 

 

 

 

 

New Developer
Posts: 24
Registered: ‎07-29-2008
My Device: Not Specified

Re: updating UI from system thread (ui engine accessed without holding the event lock)

Thanx So man would not have been able to solve the problem if i dint find this post...
Developer
Posts: 68
Registered: ‎10-26-2008
My Device: Not Specified

Re: updating UI from system thread (ui engine accessed without holding the event lock)

Hi, I'm pretty new to Blackberry UI development.  Is there a good tutorial somewhere that explains how the UI works?
Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: updating UI from system thread (ui engine accessed without holding the event lock)

generally synchronize on the eventlock or invokelater is sufficient.
in the case of some listeners you are not in the context of your application. synchonize or calling invokelater needs to be done on your UiApplication object (as correctly stated in a previous reply).
if the listener is registered in another process, for example an autostart project (alternative entry point), you don't have an application object and need to use different approachs, see the knowledge base for some ideas.
other approaches include application manager or global events.
----------------------------------------------------------
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: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: updating UI from system thread (ui engine accessed without holding the event lock)

I guess you could think about this from a larger perspective too- why bother changing a gui

component when it is unlikely you will ever display it? That is, rather than grabbing a lock

for a speculative update, you may want to consider queing the update(s) in a private

place and then updating the gui once you have something that is likely to be viewed.

I guess you could make some response time arguments- why not just do the update

while the user isn't waiting foryour screen to display if you had a really complicated update

and wanted the user to blame some other app for the slow phone (LOL).

 

Generally locks carry some risk/overhead and by design force others to wait.

If it is that much trouble to get the lock, you may just want to wait.