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
jromanoski
Posts: 90
Registered: ‎09-07-2010
My Device: Not Specified
Accepted Solution

Push and Pop screen in Thread

Hello,

 

I'm running Browser Field demo from 4.6, and i want to push some wait Dialog before the thread starts and pop it as soon as i get the first event from the thread. Something like:

 

 

public Object eventOccurred(Event event) {
		
		if (waitSc != null) {
			try {
				thread.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			UiApplication.getUiApplication().invokeLater(new Runnable() {
				public void run() {
					UiApplication.getUiApplication().popScreen(waitSc);
					waitSc = null;
					this.notifyAll();
				}
			});
		}
}

 

 

But this is giving me IllegalMonitorStateException. Does anybody know what i am doing wrong?

 

Thanks in advane.

Please use plain text.
Developer
simon_hain
Posts: 16,010
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Push and Pop screen in Thread

maybe this.notifyAll? this refers to the runnable

----------------------------------------------------------
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
Please use plain text.
Developer
jromanoski
Posts: 90
Registered: ‎09-07-2010
My Device: Not Specified

Re: Push and Pop screen in Thread

So how to notify the thread that has to resume his activity? 

Please use plain text.
Developer
jromanoski
Posts: 90
Registered: ‎09-07-2010
My Device: Not Specified

Re: Push and Pop screen in Thread

Sorry, the sentence which is giving me the error is the "thread.wait()" sentence.

Please use plain text.
Developer
simon_hain
Posts: 16,010
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Push and Pop screen in Thread

from where is eventOccured called?

----------------------------------------------------------
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
Please use plain text.
Developer
jromanoski
Posts: 90
Registered: ‎09-07-2010
My Device: Not Specified

Re: Push and Pop screen in Thread

[ Edited ]

Thanks for your help, simon.

 

It's called from a class which extends Thread. Here is the code of the BrowserView class:

 

 

 

public abstract class BrowserView extends View implements BrowserViewInterface,
		RenderingApplication {

	private String _query;

	private static final String REFERER = "referer";

	protected Configuration config = Configuration.getInstance();
	protected RenderingSession _renderingSession;
	private HttpConnection _currentConnection;
	private BBServer server = BBServer.getInstance();

	private WaitDialog waitSc = new WaitDialog("Please wait...");

	PrimaryResourceFetchThread thread;

	public BrowserView(Controller controller) {
		super(controller, MainScreen.NO_VERTICAL_SCROLL);
	}

	public void show(final String constants, String title) {

		super.show(title);
		// final BrowserView bView = this;
		// Runnable run = new Runnable() {
		// public void run() {
		UiApplication.getUiApplication().pushScreen(waitSc);
		_renderingSession = RenderingSession.getNewInstance();
		thread = new PrimaryResourceFetchThread(constants, null, null, null,
				this);
		_renderingSession.getRenderingOptions().setProperty(
				RenderingOptions.CORE_OPTIONS_GUID, 17000, true);
		thread.start();
		// UiApplication.getUiApplication().popScreen(waitSc);
		// }
		// };
		// PleaseWaitPopupScreen.showScreenAndWait(run, "Getting URL " +
		// "HOLA");
		// UiApplication.getUiApplication().invokeLater(run);
	}

        public Object eventOccurred(Event event) {
		int eventId = event.getUID();
		System.out.println("EVENT ID: " + eventId);
		if (waitSc != null) {
			try {
				thread.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
				System.out.println("ERROR: " + e.getMessage());
			}
			UiApplication.getUiApplication().invokeLater(new Runnable() {
				public void run() {
					UiApplication.getUiApplication().popScreen(waitSc);
					waitSc = null;
					this.notifyAll();
				}
			});
		}

          (...)
       }
}

 

And here is the code of the Thread class:

 

 

 

public class PrimaryResourceFetchThread extends Thread {

	private BrowserView _application;
	private Event _event;
	private byte[] _postData;
	private HttpHeaders _requestHeaders;
	private String _url;
	private BBServer server = BBServer.getInstance();

	public PrimaryResourceFetchThread(String url, HttpHeaders requestHeaders,
			byte[] postData, Event event, Screen screen) {
		_url = url;
		_requestHeaders = requestHeaders;
		_postData = postData;
		_application = (BrowserView) screen;
		_event = event;
	}

	public void run() {
		HttpConnection connection =    server.makeConnectionBrowser(_url,
				_requestHeaders, _postData);
		_application.processConnection(connection, _event);
	}
}

 

Again, thanks for yout help!

 

 

Please use plain text.
Developer
simon_hain
Posts: 16,010
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Push and Pop screen in Thread

i see. i think you call wait on an object you did not synchronize on.

you get an exception because you do not own the monitor

 

check

http://www.jchq.net/tutorial/07_03Tut.htm

----------------------------------------------------------
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
Please use plain text.
Developer
jromanoski
Posts: 90
Registered: ‎09-07-2010
My Device: Not Specified

Re: Push and Pop screen in Thread

Simon i'm trying it with this thing you told me but i think i'm getting confussed more and more.

 

First of all, i want to know one thing: can i push and pop screens inside a thread? If not, how should i do it?

Please use plain text.
Developer
simon_hain
Posts: 16,010
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Push and Pop screen in Thread

yes, you can. you have to use invokelater, invokeandwait 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
Please use plain text.
Developer
mreed
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Push and Pop screen in Thread

[ Edited ]

Calls to wait() and notify() need to be wrapped in a synchronized block with some lock object specified. Call the wait() and notify() methods on the lock object.

 

wait() is a blocking operation, so calling it before your invokeLater will prevent the invokeLater from being called. Is that what you want?

Please use plain text.