![]() |
Problems with a timer app that is run in a Background app Hi I am having a background app that has got a timer do some tasks every few seconds But when I run it , I think that the timer app is being invoked a few times before the time period set in the timer has arrived I had set a public value and incremented it in the timer and while printing it out , I can see that the value is the same for a number of times Does anyone know why it is so and how I can avoid it ? |
My translation of this post is "my code is broken, can anybody tell me how to fix it"? Please do a better job of describing your problem. |
:-o If I could have only even understood the problem , it would have been so better The problem is this Every time my timer carries out the task at the specfied time , I think it is being invoked a few times continously at a stretch The code at the first few lines seems to get executed many times before the final code lines of the function is reached And as the timer is still kept enabled , this goes on until I exit the simulator I tried by declaring the timer function synchronized , but it is still not working |
Don't under stand the phrase "I think it is...." - don't you know? Have you placed strategic breakpoints? When you say "timer", are you referring to the TimerTask execution, or are you timing this yourself with a thread? |
I am invoking the TimerTask function No , I didn't use any Breakpoints |
You need to use the available tools. Set a strategic break-point where you think the problem starts, then single-step the code until you understand the execution path and the root cause of the issue. |
Sounds like a synchronization/multi-threading issue. Are you sure the method is not invoked continuously before it completes? Try adding the "synchronized" keyword to the method signature: e.g: public synchronized void myMethodFoobar(){.....} |
Hello PuguaSoft Thats what I too was thinking Do you know the execution method of timer like how many threads are created etc ? This code is inside a Background listener app I tried using synchronized like below but there was NO change in the result at all Code: public synchronized void run() { |
This is the code that I use the timer to execute Code: when I run it I get result like the following ( I exited off the simulator after some time ) Code: Timer has been invoked What I EXPECTED was like this Code: Timer has been invoked |
bump Anyone please ? |
Hi MobileDeveloperUK, What device and OS version are you running? I tried your code on a 8300 (OS 4.5) simulator and it works fine. |
Hello Puguasoft I am also building it on JDE 4.5 as well as its default Simulator 8300 I am using Java 4.0 |
Hi I got it solved finally It was outside an if loop and so was getting invoked all the time the if loop was invoked I am sorry for the trouble |
Problem is not clear for the above code. Please try to change your timer frequency value and post the results if you observe any change in the output. However, to get a fast working timer task code, Try to define the timer variable inside the TimerTask class definition and use schedule API on it. Example: timer.schedule(this, 2500, 2500). |
| All times are GMT -5. The time now is 07:22 PM. |
Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.