Improve company productivity with a Business Account.Sign Up

x
?
Solved

C# - 2 timers with Elapsed Time.

Posted on 2013-05-26
5
Medium Priority
?
1,048 Views
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.
0
Comment
Question by:ocaccy
  • 2
  • 2
5 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 668 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.

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
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 1332 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;
            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
 

Author Comment

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

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 1332 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.
0
 

Author Closing Comment

by:ocaccy
ID: 39199907
Got it, thanks.
0

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Introduction to Processes

601 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