-
December 6th, 2011, 01:17 PM
#1
Timer problems
I have a timer that sends data to a text file every 5 minutes of the hour. The data, however, gets sent 1 second later every time. For example, it will send at 10:05:36 one time, then the next time it will send at 10:05:37. Is there a way I can fix it so it sends at the same increment?
-
December 6th, 2011, 01:50 PM
#2
Re: Timer problems
Post the relevant code so we can see what is really going on.
-
December 6th, 2011, 01:58 PM
#3
Re: Timer problems
try System.Timers.Timer instead of System.Windows.Forms.Timer
-
December 6th, 2011, 02:28 PM
#4
Re: Timer problems
I am using thread.sleep in my code. Is there better code to set up a timer?
-
December 6th, 2011, 02:37 PM
#5
Re: Timer problems
Sleep specifies a minimum amount of time to sleep for. It only checks that at least that much time has passed before resuming. It is perfectly alright for it to wait longer than you specify. Since you are not using a real-time OS, there is no way to guarantee an exact amount of time.
-
December 6th, 2011, 03:38 PM
#6
Re: Timer problems
Is there any solution to get a precise interval because my program will only send data if the time is divisible by 5 minutes?
-
December 6th, 2011, 04:01 PM
#7
Re: Timer problems
try something like this:
Code:
DateTime last = DateTime.Now;
for (; ; )
{
while (DateTime.Now - last < TimeSpan.FromMinutes(5))
{
Thread.Sleep(100); //sleep a bit to not saturate the CPU
}
last = DateTime.Now;
SendMyData(last.ToString());
}
-
December 7th, 2011, 10:06 AM
#8
Re: Timer problems
I also have another issue with this program. I am trying to send data at a certain time, so I used a do while loop.
do{
//code
}while(time.ToString("HHmm") != "1000")
However, it does not run the rest of the code at 10:00. What should I do here?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|