Link to home
Start Free TrialLog in
Avatar of ocaccy
ocaccyFlag for Japan

asked on

C# - Always run an app with the same interval.

Hello,

I use timers in my app, but after a while , begins to have delays on timers, these delays will grow over the time.

I throught of some solutions to this problem, and if anyone has other, I would learn.

1st - We always run function at the same time according to the system time.
2nd - Everyday reset the timer.

How to call a function in accordance with the system clock?
How to repeat this function every 2 hours until the funtion is canceled?

Thanks in advance,
ocaccy

"delay captured"
 
2013/6/24 - 7:57:00	first function
2013/6/24 - 8:00:00	second function
2013/6/24 - 8:00:10	third function
2013/6/24 - 8:12:00	fourth function
	
2013/6/24 - 9:57:00	first function
2013/6/24 - 10:00:00	second function
2013/6/24 - 10:00:10	third function
2013/6/24 - 10:12:00	fourth function
	
2013/6/24 - 11:57:01	first function
2013/6/24 - 12:00:01	second function
2013/6/24 - 12:00:11	third function
2013/6/24 - 12:12:01	fourth function
	
2013/6/24 - 13:57:01	first function
2013/6/24 - 14:00:01	second function
2013/6/24 - 14:00:11	third function
2013/6/24 - 14:12:01	fourth function
	
2013/6/24 - 15:57:04	first function
2013/6/24 - 16:00:05	second function
2013/6/24 - 16:00:15	third function
2013/6/24 - 16:12:05	fourth function
	
2013/6/24 - 17:57:04	first function
2013/6/24 - 18:00:04	second function
2013/6/24 - 18:00:14	third function
2013/6/24 - 18:12:04	fourth function
	
2013/6/24 - 19:57:06	first function
2013/6/24 - 20:00:06	second function
2013/6/24 - 20:00:16	third function
2013/6/24 - 20:12:06	fourth function
	
2013/6/24 - 21:57:09	first function
2013/6/24 - 22:00:09	second function
2013/6/24 - 22:00:19	third function
2013/6/24 - 22:12:09	fourth function
	
2013/6/24 - 23:57:11	first function
2013/6/25 - 0:00:11	second function
2013/6/25 - 0:00:21	third function
2013/6/25 - 0:12:11	fourth function
	
2013/6/25 - 1:57:13	first function
2013/6/25 - 2:00:13	second function
2013/6/25 - 2:00:23	third function
2013/6/25 - 2:12:13	fourth function

Open in new window

SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ocaccy

ASKER

Code OK. embed=snippet 8257405

How to reset the timers after processing?

System scheduler?  Tell me about it, please.

            timer_sensorLoop.Interval=7200000;
            timer_sensorCopy.Interval=900000;
            timer_preSensor.Interval=180000;
            timer_SENSOR.Interval=30000;

        #region timer_sensorLoop_Tick
        private void timer_sensorLoop_Tick(object sender,EventArgs e)
            {
            counterSENSOR=(Convert.ToInt32(Settings.ment.s_nUD_ILS*60));

            save1Time=save1Time+DateTime.Now.ToLongDateString()+" - "+DateTime.Now.ToLongTimeString()+","+"Sensor Copy Start 'sensorLoop'";
            Event_save();

            if(timer_sensorCopy.Enabled!=true) { timer_sensorCopy.Start(); }
            if(timer_preSensor.Enabled!=true) { timer_preSensor.Start(); }
            }
        #endregion

        #region timer_preSensor_Tick
        private void timer_preSensor_Tick(object sender,EventArgs e)
            {
            save1Time=save1Time+DateTime.Now.ToLongDateString()+" - "+DateTime.Now.ToLongTimeString()+","+"Sensor Clear Start 'preSensor'";
            Event_save();

            timer_preFilter.Stop(); timer_filterCopy.Stop(); timer_FILTER.Stop();
            timer_SENSOR.Start();
            timer_preSensor.Stop();
            label11.Text="preSensor: Turn ON the SENSOR.";
            }
        #endregion

        #region timer_SENSOR_Tick
        private void timer_SENSOR_Tick(object sender,EventArgs e)
            {
            save1Time=save1Time+DateTime.Now.ToLongDateString()+" - "+DateTime.Now.ToLongTimeString()+","+"Sensor Clear Stop 'SENSOR'";
            Event_save();

            timer_SENSOR.Stop();
            label11.Text="SENSOR: Turn OFF the SENSOR.";
            }
        #endregion

        #region timer_sensorCopy_Tick
        private void timer_sensorCopy_Tick(object sender,EventArgs e)
            {
            save1Time=save1Time+DateTime.Now.ToLongDateString()+" - "+DateTime.Now.ToLongTimeString()+","+"Sensor Copy Stop 'sensorCopy'"+Environment.NewLine;
            Event_save();
            timer_sensorCopy.Stop();
            }
        #endregion

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ocaccy

ASKER

Thank you julianH and Idle_Mind.

I changed the code to run in 1 minute.
I'll leave running for a few days.
I'm recording the data for analysis.
This working.

    public partial class Form1:Form
        {
        private DateTime DT_sensorLoop;
        int x=0;

        public Form1()
            {
            InitializeComponent();
            //DT_sensorLoop=DateTime.Today.AddHours(DateTime.Now.Hour).AddHours(1);
            //DT_sensorLoop=DateTime.Now.AddMinutes(DateTime.Now.Minute).AddMinutes(1);
            DT_sensorLoop=DateTime.Now.AddMinutes(1);
            timer_sensorLoop.Interval=(int)DT_sensorLoop.Subtract(DateTime.Now).TotalMilliseconds;
            timer_sensorLoop.Start();
            label1.Text="DT_sensorLoop: "+DT_sensorLoop.ToString();
            label2.Text="timer_sensorLoop.Interval: "+timer_sensorLoop.Interval.ToString();
            bLOG_save(); x++;
            }

		private void timer_sensorLoop_Tick(object sender,EventArgs e)
            {
            timer_sensorLoop.Stop();
            //DT_sensorLoop=DT_sensorLoop.AddHours(2);
            DT_sensorLoop=DT_sensorLoop.AddMinutes(1);
            timer_sensorLoop.Interval=(int)DT_sensorLoop.Subtract(DateTime.Now).TotalMilliseconds;
            timer_sensorLoop.Start();
            label3.Text="Timer "+x+" minute(s)";
            label2.Text="timer_sensorLoop.Interval: "+timer_sensorLoop.Interval.ToString();
            label4.Text="DT_sensorLoop: "+DT_sensorLoop.ToString();
            bLOG_save();
            x++;
            }
		}

Open in new window

Avatar of ocaccy

ASKER

Thank you.
ocaccy
You are welcome - thanks for the points.