Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Example: Total time 10 Minutes

ON TIME:2000 millisecond
OFF TIME:5000 millisecond
ON TIME:2000 millisecond
OFF TIME:5000 millisecond
--------------------
--------------------
repeating For Total Time
Posted

1 solution

Hi,

this code should do what you want:
C#
System.Timers.Timer tmr = new System.Timers.Timer();
DateTime startTime;

private void Form1_Load(object sender, EventArgs e)
{
    tmr.Interval = 2000;
    tmr.Elapsed += new System.Timers.ElapsedEventHandler(tmr_Elapsed);
    startTime = DateTime.Now;
    tmr.Start();
}

void tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    if (tmr.Interval == 2000)
        tmr.Interval = 5000;
    else
        tmr.Interval = 2000;

    Console.WriteLine("Elapsed Seconds: " + (DateTime.Now - startTime).TotalSeconds.ToString());

    //Stop the Timer after 10 minutes
    if ((DateTime.Now - startTime).TotalMinutes > 10)
        tmr.Stop();
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Feb-11 2:04am    
Good enough, 5.
--SA
nitendrasingh 9-Feb-11 4:36am    
Precisely I have two textbox where user enter Ontime and offtime so it will show in listbox printed
depends on user input(milliseconds) with messages like ontime for 2 sec and offtime for 5 sec.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900