Solved

C# - 2 timers with Elapsed Time.

Posted on 2013-05-26
5
709 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 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 167 total points
Comment Utility
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 29

Accepted Solution

by:
anarki_jimbel earned 333 total points
Comment Utility
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
Comment Utility
The answers seemed the way. But, I could not put either of them in practice, can help me with an example?
0
 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 333 total points
Comment Utility
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
Comment Utility
Got it, thanks.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
This is an explanation of a simple data model to help parse a JSON feed
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now