BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 02-11-2010, 03:22 AM   #1 (permalink)
Thumbs Must Hurt
 
Join Date: Jan 2010
Model: 8300
PIN: N/A
Carrier: T-Mobile
Posts: 62
Post Thanks: 1
Thanked 0 Times in 0 Posts
Default Problems with a timer app that is run in a Background app

Please Login to Remove!

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 ?
Offline  
Old 02-11-2010, 08:37 AM   #2 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default

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.
Offline  
Old 02-11-2010, 01:49 PM   #3 (permalink)
Thumbs Must Hurt
 
Join Date: Jan 2010
Model: 8300
PIN: N/A
Carrier: T-Mobile
Posts: 62
Post Thanks: 1
Thanked 0 Times in 0 Posts
Default



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
Offline  
Old 02-11-2010, 04:35 PM   #4 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default

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?
Offline  
Old 02-13-2010, 02:16 AM   #5 (permalink)
Thumbs Must Hurt
 
Join Date: Jan 2010
Model: 8300
PIN: N/A
Carrier: T-Mobile
Posts: 62
Post Thanks: 1
Thanked 0 Times in 0 Posts
Default

I am invoking the TimerTask function

No , I didn't use any Breakpoints
Offline  
Old 02-13-2010, 11:53 AM   #6 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default

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.
Offline  
Old 02-13-2010, 06:57 PM   #7 (permalink)
New Member
 
Join Date: Feb 2010
Model: 8900
PIN: N/A
Carrier: ATT
Posts: 13
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

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(){.....}
Offline  
Old 02-14-2010, 01:35 AM   #8 (permalink)
Thumbs Must Hurt
 
Join Date: Jan 2010
Model: 8300
PIN: N/A
Carrier: T-Mobile
Posts: 62
Post Thanks: 1
Thanked 0 Times in 0 Posts
Default

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() {

 //String prevEntry;
 
 synchronized (this)

{

// My codes 
 

}

}
Offline  
Old 02-14-2010, 04:50 AM   #9 (permalink)
Thumbs Must Hurt
 
Join Date: Jan 2010
Model: 8300
PIN: N/A
Carrier: T-Mobile
Posts: 62
Post Thanks: 1
Thanked 0 Times in 0 Posts
Default

This is the code that I use the timer to execute

Code:


 synchronized (this)
 
 {
                 
 
Timer timer = new Timer();

TimerTask updateTask;
 
 


  
      
updateTask = new TimerTask() {
 
 
 int skppdTms ;
 
   
 public  synchronized void run() {

  System.out.println(" Timer has been invoked " );
 
 if ( skppdTms ==5)
       
       {
   
  skppdTms=0;
          
System.out.println("  skppdTms is 5  :   " );
 
          }
 
  skppdTms=skppdTms+1;
    
    System.out.println(" Skipped Times " + skppdTms);
  
        
}
      

};

timer.scheduleAtFixedRate(updateTask, 2500, 2500);

  }

when I run it I get result like the following

( I exited off the simulator after some time )
Code:
 Timer has been invoked 
 Skipped Times 1
 Timer has been invoked 
 Skipped Times 1
 Timer has been invoked 
 Timer has been invoked 
 Timer has been invoked 
 Timer has been invoked 
 Timer has been invoked 
 Skipped Times 1
 Skipped Times 1
 Skipped Times 1
 Skipped Times 1
 Skipped Times 1
 Timer has been invoked 
 Skipped Times 2
 Timer has been invoked 
 Skipped Times 2
 Timer has been invoked 
 Skipped Times 2
 Timer has been invoked 
 Skipped Times 2
 Timer has been invoked 
 Timer has been invoked 
 Timer has been invoked 
 Skipped Times 2
 Skipped Times 2
 Skipped Times 2
 Timer has been invoked 
 Timer has been invoked 
 Timer has been invoked 
 Timer has been invoked 
 Timer has been invoked 
 Timer has been invoked 
 Timer has been invoked 
 Skipped Times 3
 Skipped Times 3
 Skipped Times 3
 Skipped Times 3
 Skipped Times 3
 Skipped Times 3
 Skipped Times 3
 Timer has been invoked 
 Skipped Times 4
 Timer has been invoked 
 Skipped Times 4
 Timer has been invoked 
 Skipped Times 4
 Timer has been invoked 
 Skipped Times 4
 Timer has been invoked 
 Timer has been invoked 
 Timer has been invoked 
 Skipped Times 4
 Skipped Times 4
 Skipped Times 4
 Timer has been invoked 
 Skipped Times 5
 Timer has been invoked 
 Skipped Times 5
 Timer has been invoked 
 Skipped Times 5
 Timer has been invoked 
 Skipped Times 5
 Timer has been invoked 
 Skipped Times 5
 Timer has been invoked 
 Skipped Times 5
 Timer has been invoked 
 Skipped Times 5
 Timer has been invoked 
  skppdTms is 5  :   
 Skipped Times 1
 Timer has been invoked 
  skppdTms is 5  :   
 Skipped Times 1
 Timer has been invoked 
 Timer has been invoked 
 Timer has been invoked 
 Timer has been invoked 
 Timer has been invoked 
  skppdTms is 5  :   
  skppdTms is 5  :   
  skppdTms is 5  :   
  skppdTms is 5  :   
  skppdTms is 5  :   
 Skipped Times 1
 Skipped Times 1
 Skipped Times 1
 Skipped Times 1
 Skipped Times 1

What I EXPECTED was like this

Code:
Timer has been invoked 
 Skipped Times 1
 Skipped Times 2
 Skipped Times 3
 Skipped Times 4
 Skipped Times 5
skppdTms is 5  :
Timer has been invoked 
 Skipped Times 1
Offline  
Old 02-15-2010, 05:24 PM   #10 (permalink)
Thumbs Must Hurt
 
Join Date: Jan 2010
Model: 8300
PIN: N/A
Carrier: T-Mobile
Posts: 62
Post Thanks: 1
Thanked 0 Times in 0 Posts
Default

bump

Anyone please ?
Offline  
Old 02-16-2010, 03:42 AM   #11 (permalink)
New Member
 
Join Date: Feb 2010
Model: 8900
PIN: N/A
Carrier: ATT
Posts: 13
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
Offline  
Old 02-16-2010, 04:42 AM   #12 (permalink)
Thumbs Must Hurt
 
Join Date: Jan 2010
Model: 8300
PIN: N/A
Carrier: T-Mobile
Posts: 62
Post Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hello Puguasoft

I am also building it on JDE 4.5 as well as its default Simulator 8300

I am using Java 4.0
Offline  
Old 02-16-2010, 05:11 AM   #13 (permalink)
Thumbs Must Hurt
 
Join Date: Jan 2010
Model: 8300
PIN: N/A
Carrier: T-Mobile
Posts: 62
Post Thanks: 1
Thanked 0 Times in 0 Posts
Default

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
Offline  
Old 02-16-2010, 05:28 AM   #14 (permalink)
Knows Where the Search Button Is
 
Join Date: Apr 2009
Model: 8800
PIN: N/A
Carrier: Airtel
Posts: 31
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

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).
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.