Author Topic: DO something every second  (Read 3237 times)

ciuffolo

  • Guest
DO something every second
« on: December 15, 2012, 03:39:48 pm »
Hi,i only want to know how to do something every second, such as OnTimer() does the same things for 30 times at second.
I tried with a counter that does the thing i want 1 time on 30 of OnTImer, but i realized is not the best way.

graham

  • Guest
Re: DO something every second
« Reply #1 on: December 16, 2012, 03:59:47 am »
i think it's the only way to do it. i can't see what's wrong with it.

Code: [Select]
int Tick = 0;

void OnTimer()
{
   Tick++;
   if (Tick % 30 == 0) {
   // This block will be executed once per second.
   }
}

nednones

  • Guest
Re: DO something every second
« Reply #2 on: December 16, 2012, 05:09:33 am »
Don't forgot to reset Tick whilst executing your if loop, e.g.

int Tick = 0;

void OnTimer()
{
   Tick++;
   if (Tick % 30 == 0)
   {
        Tick = 0;
        // This block will be executed once per second.
   }
}

graham

  • Guest
Re: DO something every second
« Reply #3 on: December 16, 2012, 05:25:23 am »
there is no need for the statement Tick = 0;

ciuffolo

  • Guest
Re: DO something every second
« Reply #4 on: December 16, 2012, 07:54:06 am »
Yes, i did it.
Thanks anyway.

glina126

  • Guest
Re: DO something every second
« Reply #5 on: December 19, 2012, 12:23:25 pm »
there is no need for the statement Tick = 0;

c++ sets any new variable to 0 automatically, your right. But, it is a VERY good practice to do it anyway, just to avoid unforeseen circumstances ;)

graham

  • Guest
Re: DO something every second
« Reply #6 on: December 19, 2012, 12:46:07 pm »
i was talking about this Tick = 0:

Code: [Select]
if (Tick % 30 == 0)
   {
        Tick = 0;

nednones

  • Guest
Re: DO something every second
« Reply #7 on: December 19, 2012, 04:24:45 pm »
Graham, you're wrong on this one, sorry! Although the code is executed once per second, this is because of the modulus divide. However! this is bad code practice because if you test what you've stated you'll see that tick is always incremented and is never controlled such that you will eventually get an integer overflow (int var >= 32767) which will crash your app. You should always initialize your variable to zero to ensure that you don't exceed the parameters maximum hold value. So the correct code should be...

int tick = 0;

void onTimer()
{
   Tick++;

   if(Tick == 30)
   {
        //Execute code
        Tick = 0;
    }
}

graham

  • Guest
Re: DO something every second
« Reply #8 on: December 19, 2012, 05:11:17 pm »
Tick won't overflow until your app has been running for more than a year.

nednones

  • Guest
Re: DO something every second
« Reply #9 on: December 20, 2012, 12:31:17 am »
That's not the point, the fact that you don't tidy up your parameters in your app is bad practice. And just to note, all compilers don't initialise variable types to zero, they allocate a section of memory in the memory stack which when declared may contain garbage so don't rely on this fact

graham

  • Guest
Re: DO something every second
« Reply #10 on: December 20, 2012, 02:22:14 am »
Quote
And just to note, all compilers don't initialise variable types to zero, they allocate a section of memory in the memory stack which when declared may contain garbage so don't rely on this fact

I know, that's why I initialise it with
Code: [Select]
int Tick = 0;.

Nico91

  • Guest
Re: DO something every second
« Reply #11 on: December 28, 2012, 03:36:47 am »
your tick become 32000 in 17 minuts.

graham

  • Guest
Re: DO something every second
« Reply #12 on: December 28, 2012, 04:44:13 am »
What is special about 17 minutes?

Nico91

  • Guest
Re: DO something every second
« Reply #13 on: December 28, 2012, 08:38:47 am »
He said : you will eventually get an integer overflow (int var >= 32767) which will crash your app.

In 17 minut the app crash.

graham

  • Guest
Re: DO something every second
« Reply #14 on: December 28, 2012, 09:29:23 am »
Integers don't overflow at 32000.  The number is more than one billion!