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: 81
Registered: ‎03-22-2013
My Device: 9900,9930,9790,9780,9330
My Carrier: Blackberry
Accepted Solution

How to run main class thread when PopupScreen is pushed??

Hello All,

 

I have created custom Popup in my Application. on click of Button it shows properly .

But in my  Main screen  i have used webservice. Screen contents are shows depends on webservice respons.

 

My problem is that when i click on button popup comes but threat used for webservice call stops its execution because due to popup screen my mainscreen becomes not in focus for execution. and when i remove popup screen my mainclass resumes its execution and result is shows  properly. 

 

Please anyone tell me how it be possible that in presence of Popup screen my thread for Webservice call will be execute??

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

Re: How to run main class thread when PopupScreen is pushed??

I suspect your problem is that you are using the Event Thread to do your Web call.  Your popup screen presumably is modal, in that it blocks processing until the user responds.  This then stops your web service.

 

Regardless of the this, you need to get your web service off the Event Thread.  Have it run in its own Thread and then update the User Interface when you get the response.  This typically means you have to display some 'loading' or 'please wait' type screen while the web processing is going on. 

 

More on the Event Thread:

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

New Developer
Posts: 81
Registered: ‎03-22-2013
My Device: 9900,9930,9790,9780,9330
My Carrier: Blackberry

Re: How to run main class thread when PopupScreen is pushed??

Thanks Peter.

 

I have implemented same way as you have said in this post . I  have shows Loading screen still webservice response. I have created  seperate  thread for webservice request and response . But its not working  thread stops its execution when Popup screen is in focus.  

 

Please tell me any  other solution for that.

 

 

 

 

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

Re: How to run main class thread when PopupScreen is pushed??

The approach I said is the correct apprach, so my suspicion is that you have not implemented it correctly.

 

Here is a way to test.  Add this code in any code that you are think is not running on the Event Thread:

 

if ( Application.isEventDispatchThread() (

Dialog.alert("Oppsss, this code should not be running on the Event Thread");

}

 

Obviously if you see the Dialog, then the code is running on the Event Thread.

 

If you don't, then post your code, perhaps we can figure out what you have done wrong. 

New Developer
Posts: 81
Registered: ‎03-22-2013
My Device: 9900,9930,9790,9780,9330
My Carrier: Blackberry

Re: How to run main class thread when PopupScreen is pushed??

Thanks Peter you are simply great !!

I have only replace Dialog by my custom popup and it works.

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

Re: How to run main class thread when PopupScreen is pushed??

I still wonder if you have a problem with code that should be running in the background, actually running on the Event Thread.

 

The two most common causes of this are:

 

a) Thinking that invokeLater(....) actually starts a separate Thread - it doesn't

b) After creating a Thread, running it using <thread>.run() instead of <thread>.start().

 

As noted, you can put the test code I supplied in anything you think should be running in the background - if you see the Dialog, then it is NOT running in the background.  

 

If in fact this problem is solved, don't forget t mark it as solved - you will find the instructions and other helpful information in the Help link near the top of the screen. 

New Developer
Posts: 81
Registered: ‎03-22-2013
My Device: 9900,9930,9790,9780,9330
My Carrier: Blackberry

Re: How to run main class thread when PopupScreen is pushed??

Here is my sample code . In that i have created thread for calling webservice and parse returned result. While thrad buzy to return result i am showing loading message . and inbetween if user clicks on Btn_PopUp it shows custom popup.


if (field == Btn_PopUp)
{

if ( Application.isEventDispatchThread()) {

UiApplication.getUiApplication().pushScreen(new PopUpFilterOptions());

}

}

//This code for webservice call

public void get_WebResponse()
{

try
{
new Thread(new Runnable()
{
public void run()
{
String result = "";
String val =getConnectionString();
//if Connection available
if (val != "")
{
result = sendRequest("" ,"");
System.out.println("Result :" + result);
}
else
Dialog.inform("Please try again");
if (result != "")
{

parseJSON(result);

}
else
{
// the size of the list is zero
synchronized (Application.getEventLock())
{
vfm_main.deleteAll();
LabelField label = new LabelField()
{
protected void paint(Graphics g)
{
g.setColor(Color.BLACK);
g.clear();
super.paint(g);
}
};
label.setText("Couldn't connect to Internet OR the network is not avaliable!");
vfm_main.add(label);
}
}
}
}).start();
}
catch (Exception e)
{
e.getMessage();
}
}

public void parseJSON(String jsonTxt)
{
try
{
// Here is my Parsing code for parse result

//Adding GuI according to parse result
synchronized (Application.getEventLock())
{
if (vectResponse.size() > 0)
{
vfm_main.delete(_ourAnimation);
add_SongData();
}
}
}
catch (final JSONException je)
{
e.getMessage();
}
catch (Exception e)
{
e.getMessage();
}
}

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

Re: How to run main class thread when PopupScreen is pushed??

Your code seems to confirm my suspicions.  If you can do UI actions such as

 

Dialog.inform("Please try again");

 

and

 

add_SongData();

 

then the code must be running in the Event Thread.  Try my test code to prove it.