• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

stopping Timer after certain number of actions

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This is my first post here, so please bear with me.

I want to make my simple program print "Action" 5 times and then stop. The code below will print "Action" once per second forever. If I un-comment the timer.cancel(); it stops before it even prints once. I've tried putting the timer inside a for loop and inside a while loop, but can't make them work.

What am I missing?

Thanks in advance.

- Johnny

 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, welcome to the ranch
I enclosed your source in [code] tags. Please UseCodeTags the next time you post some code.

About your problem, why don't you put the counter variable inside the anonymous TimerTask, increment it and cancel when it reaches five ?
 
John Greenauer
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply

I'm not having much success with that. The code below causes "Action to be print 5 times immediately, every second (after the 1 second delay). I believe that the 1 second delay in the timer method does not prevent the computer from running through the for loop quickly. So, 5 timers are created in less than a second and they all activate after the one second delay. Also, if the timer.cancel(); is un-commented, the program prints nothing because it ends before the one second delay has run it's course. I've tried a slightly different version of this with the timer.cancel(); placed within an if( count == 4) loop. It also prints nothing.

Perhaps the timer method will not work the way I want it to. Maybe, I need a different method. Any thoughts?



 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not talking about looping 5 times in the TimerTask. The Timer will call the task, that's its job, not yours. What I'm talking about is to put the "int counter" inside the TimerTask, to increment it when the task is called (i.e in the run() method), and to cancel the timer when the counter reaches 5.
 
John Greenauer
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
O.K. I finally got it. That definitely took longer than it should have.

I was getting an error message with timer.cancel(); , but System.exit(0); worked straight off. I've put the final product below for any rookies (like me) that might look for it.

Thanks for the help.

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I was getting an error message with timer.cancel(); , but System.exit(0); worked straight off.


Dooh this.cancel() would do the trick.
 
John Greenauer
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahh. Yes, that does work. I haven't gotten to "this" in my book yet. I see that it stops printing "Action" without stopping the program. I can see how that's probably a better option in most cases. Thanks a lot.
reply
    Bookmark Topic Watch Topic
  • New Topic