If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

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
vingilot
Posts: 224
Registered: ‎03-29-2012
My Device: 9900
My Carrier: Wind

Stop and Start Threading

[ Edited ]

Hey all, trying to get a thread to start on a button click, causing a "searching" popupscreen to show, then when the bluetooth inquirycompleted method is called, it pops the searching popupscreen as shown: 

 

the button that triggers it:

engage.setChangeListener(new FieldChangeListener() {
				public void fieldChanged(Field field, int context) {
					active = true;
					repeat = false;
					UiApplication.getUiApplication().pushScreen(progressing);
					new RefreshThread().start();
				}
			});
			add(engage);
			WaitDone.setText("on entry");
			add(WaitDone);

 

 

 

 This is the run method of the thread:

 

public void run() {
			while (active == true) {
				if (repeat == false) {
					active = false;
				}
				try {
					waiting = "waiting";
					remoteDevices.removeAllElements();
					devicesList.removeAllElements();
					discoveryAgent.startInquiry(DiscoveryAgent.GIAC, Settings);

				} catch (Exception e) {
					e.printStackTrace();
				}
				if (repeat == true) {
					try {
						Thread.sleep(20000); // how long to wait before
												// searching
												// again.
					} catch (InterruptedException e) {

					}
				}
			}

 the inquiry completed method: 

public void inquiryCompleted(int discType) {
			monitor = monitor + 10;
			waiting = "done";
			UiApplication.getUiApplication().invokeLater(new Runnable() {
				public void run() {
					if (repeat == false)
					UiApplication.getUiApplication().popScreen(progressing);
					// WaitDone.setText(waiting);
				}
			});
		}

 It is supposed to work so that when you click the button, repeat equals false, so that when the thread's run method is called, it only goes through once because afterwards since repeat=false, active=false and the while loop will end. This works and it pops the popup perfectly, the issue is when I click the button again, the popup appears but it is never popped as it should be by the inquirycompleted method. I am still working on my understanding of threading and if anyone could show me my errors that would be terrific.

 

 

Please use plain text.
Developer
vingilot
Posts: 224
Registered: ‎03-29-2012
My Device: 9900
My Carrier: Wind

Re: Stop and Start Threading

I've put in a cancel button to  pop the popup if it is taking too long and have noticed that when I cancel it after having taken too long, the variable waiting=waiting, therefore it is getting hung up following waiting, on the discoveryAgent.startInquiry(DiscoveryAgent.GIAC, Settings) line which begins the bluetooth search. If I press cancel, which essentially just pops the screen, and try again, it works. Why is there a delay in some cases? If i wait  longer inbetween uses it more frequently works. Thanks

Please use plain text.