C# - 2 timers with Elapsed Time.

Posted on 2013-05-26
Last Modified: 2013-05-27
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.
Question by:ocaccy
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 167 total points
ID: 39198467
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.


private DateTime _startTime;


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

    if ((this._startTime.AddSeconds(this._timer1.Interval) - DateTime.Now).TotalMinutes < 10)

Open in new window

LVL 30

Accepted Solution

anarki_jimbel earned 333 total points
ID: 39198503
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;
            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));
                System.Diagnostics.Debug.WriteLine(string.Format("Timer2:  - missing"));


        private void button4_Click(object sender, EventArgs e)

Open in new window


Author Comment

ID: 39198678
The answers seemed the way. But, I could not put either of them in practice, can help me with an example?
LVL 30

Assisted Solution

anarki_jimbel earned 333 total points
ID: 39199693
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.

Author Closing Comment

ID: 39199907
Got it, thanks.

Featured Post

Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Six Sigma Control Plans

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question