Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 941
  • Last Modified:

C# - 2 timers with Elapsed Time.

Hi everybody.
We have 2 timers with different intervals. I'll cal them timer1 and timer2.
The two timers can not run at the same time and the timer1 is preferred over the timer2.
Say the timer1.Inteval=2 hours (2*60*60000) and timer2.Interval=50 minutes (50*60000).
When less than 10 minutes to timer1 run, the timer2 can not run.
How to do this using timer.Elapsed ?
Any better ideas?
Thanks in advance, ocaccy.
0
ocaccy
Asked:
ocaccy
  • 2
  • 2
3 Solutions
 
käµfm³d 👽Commented:
I don't believe any of the timers within .NET expose properties that give a "time remaining until next tick." I suspect you will need to track when you start the timer, and then add the interval for that timer to the start time to see (approximately) how much time remains until the next tick.

e.g.

Pseudo-code-ish
private DateTime _startTime;

...

public void Form1_Load(object sender, EventArgs e)
{
    this._startTime = DateTime.Now;
    this._timer1.Start();

    if ((this._startTime.AddSeconds(this._timer1.Interval) - DateTime.Now).TotalMinutes < 10)
    {
        this._time2.Stop();
    }
}

Open in new window

0
 
anarki_jimbelCommented:
You may try the following code.

I have two timer. For convenience - 500 and 700 ms.
I register next time when T! will tick. And if difference between T2 and next T1 <200 ms - I skip the event.

Something like that:

        DateTime nextTimer1;
        private void button3_Click(object sender, EventArgs e)
        {
            timer1.Interval = 500;
            timer2.Interval = 700;
            timer1.Start();
            timer2.Start();
            nextTimer1 = DateTime.Now.AddMilliseconds(timer1.Interval);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            nextTimer1 = DateTime.Now.AddMilliseconds(timer1.Interval);
            System.Diagnostics.Debug.WriteLine(string.Format("Timer1: {0}min, {1}s, {2}ms",DateTime.Now.Minute, DateTime.Now.Second,DateTime.Now.Millisecond));
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            TimeSpan ts = (DateTime.Now - nextTimer1);
            System.Diagnostics.Debug.WriteLine("      next T1 = " + nextTimer1.Second + "/" + nextTimer1.Millisecond + " now = " + DateTime.Now.Second + "/" + DateTime.Now.Millisecond + "  ; span = " + ts.TotalMilliseconds + "ms"); 
            if (Math.Abs(ts.TotalMilliseconds) > 200)
            {
                System.Diagnostics.Debug.WriteLine(string.Format("Timer2: {0}min, {1}s, {2}ms", DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond));
            }
            else
            {
                System.Diagnostics.Debug.WriteLine(string.Format("Timer2:  - missing"));
            }


        }

        private void button4_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            timer2.Stop();
        }

Open in new window

0
 
ocaccyAuthor Commented:
The answers seemed the way. But, I could not put either of them in practice, can help me with an example?
0
 
anarki_jimbelCommented:
What I showed is an example. basically, the algorithm is very simple:
1. You start both timers.
2. Timer 1 always does something (prints a line in my case).
    Also it sets a variable designating next time Timer 1 tick happens.
3. Time 2 is also ticking. When Timer 2 tick happens it checks when Timer 1 tick happens.
    If time from Timer 2 current tick (DateTime.Now) to expected Timer 2 tick is less than 200 ms or 1000 ms or 60000 ms etc.  - Timer 2 does nothing (in my example it prints "missing").
4. If time above is greater than some period - Timer 2 does what it has to do.

I'm not using Timer.Elapsed event, and think this is not possible.
0
 
ocaccyAuthor Commented:
Got it, thanks.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now