ocaccy
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"
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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++;
}
}
ASKER
Thank you.
ocaccy
ocaccy
You are welcome - thanks for the points.
ASKER
How to reset the timers after processing?
System scheduler? Tell me about it, please.
Open in new window