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
  • 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 29

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 29

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

770 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