Beneagle
Beneagle

Reputation: 103

timer that runs 2 of every 3 seconds c#

Is there a way to run a program every two of three seconds? I know you can make timers run every n milliseconds, but not every n of m seconds. For a very basic example, pretend I want to have two functions that both add to an integer value. One function would run two of every three seconds and add 5 to some value. The second function would add 100 to the same value, but would only run that one second that the other one doesn't run. Does this make sense?

Upvotes: 0

Views: 203

Answers (1)

Guvante
Guvante

Reputation: 19203

The simplest way would be to keep an internal representation of which second you were on and switch based on it to a different execution.

private int counter = 0;
private int whichSecond = 0;

void RunsEverySecond()
{
    if (whichSecond < 2)
    {
        counter += 5;
        whichSecond++;
    }
    else
    {
        counter += 100;
        whichSecond = 0;
    }
}

Upvotes: 5

Related Questions