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: 37
Registered: ‎05-27-2010
My Device: 9700 Storm
Accepted Solution

Timer Task - Throws RunTimeException

In my application, I have to check a method returning a boolean value(This method is connecting to a Web service method using Httpconnection). I used Timer and TimerTask, but RuntimeException is being thrown and the timer is automatically cancelled. I scheduled in the constructor like this

 

timer =  new Timer();
timer.schedule(new checkConnection(), 180000, 180000);

In every 3 minutes I have to get a message showing the returning value of the web service method. 

 

private class checkConnection extends TimerTask
{
    public checkConnection()
    {
    }

    public void run()
    {
       if(checkWebConnection(Login.strTechCode, Login.strTechPin)) 
	  Status.show("Connection Exists");
       else Status.show("Connection Lost");
    }
}

After the delay, it shows JVM Error 104 Uncaught: RuntimeException. I commented the if and else parts in run method, but the exception remains the same. Why doesn't my program show even a message in particular intervals?

 

 

--------------------------------------------------------------------------------------
Justin Aloor

Feel free to press the Kudos Button to thank the user who helped you.
Please mark posts as solved if you found a solution.
Developer
Posts: 17,019
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Timer Task - Throws RunTimeException

timertasks should not be used to execute blocking code, like networking. start an own thread to do the networking (unless you do this already).

----------------------------------------------------------
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: 37
Registered: ‎05-27-2010
My Device: 9700 Storm

Re: Timer Task - Throws RunTimeException

If timertask should not be used to execute networking code, I will have to implement separate Thread. But why can't it show a message in particular intervals? Should I add something for that?

--------------------------------------------------------------------------------------
Justin Aloor

Feel free to press the Kudos Button to thank the user who helped you.
Please mark posts as solved if you found a solution.
Developer
Posts: 17,019
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Timer Task - Throws RunTimeException

use invokelater

----------------------------------------------------------
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
Highlighted
Developer
Posts: 37
Registered: ‎05-27-2010
My Device: 9700 Storm

Re: Timer Task - Throws RunTimeException

Calling a method which uses HttpConnection inside invokeLater is practical? I checked and it's working fine !!!!  The code given below is providing the exact result I needed. Are there any side effects?

 

UiApplication.getUiApplication().invokeLater(new Runnable()
{
    public void run()
    {
        if(checkWebConnection(Login.strTechCode, Login.strTechPin)) 
            Status.show("Connection Exists");
        else Status.show("Connection Lost");
    }
}, 180000, true);				

 

 

 

--------------------------------------------------------------------------------------
Justin Aloor

Feel free to press the Kudos Button to thank the user who helped you.
Please mark posts as solved if you found a solution.
Developer
Posts: 17,019
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Timer Task - Throws RunTimeException

you should use the invokelater around the UI calls, in your case status.show, only.

networking of any kind should run on a separate thread.

if checkWebConnection directly uses networking i would suggest that you surround the whole with a new Thread and use invokelater only around the status.show parts.

----------------------------------------------------------
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