Can't match times in C#
I've got a very simple program written in C#, but the loop never exits because the times don't match.
static void Main(string[] args)
{
while (System.DateTime.Now != new System.DateTime(2011, 05, 23, 22, 17, 0))
{
}
System.Diagnostics.Process.Start(file);
}
The idea is that when the time ticks over to the specified time, then the given file will be started. However, I've tested this program with values which are, for example, just one minute ahead of the current time as reported by Windows, and it won't start the process. I've verified that the Process.Start
call is correct. Any suggestions?
Edit: No, this is not an experiment or anything of the sort. It's because I keep turning off my alarm clocks in my sleep. file
is an mp3 file, and I'm going to leave my speakers on, and I'm pretty sure that I don't possess the capacity to deal with that 开发者_JAVA百科in my sleep. First ever practical problem I solved with a program. As it possesses a rather specific purpose, I think you'll agree that the necessity of another solution is, well, limited.
Edit: I didn't realize that the DateTime type went down to that kind of precision, else I would have spotted this myself. I thought that they were only valid down to the second, and since the loop should run even in debug mode in the IDE many, many times a second, I didn't see why an exact match would be unreasonable. But, of course, if you're comparing it down to the hundred nanoseconds, it's pretty damn unlikely.
You should do
static void Main(string[] args)
{
while (System.DateTime.Now < new System.DateTime(2011, 05, 23, 22, 17, 0))
{
}
System.Diagnostics.Process.Start(file);
}
Since if you don't tick on that exact time it isn't going to ever exit the while
Edit long explanation of why != most likely won't work So actually you could write the code like this:
static void Main(string[] args)
{
DateTime fireDate = new DateTime(2011, 05, 23, 22, 17, 0);
while (System.DateTime.Now < fireDate)
{
}
System.Diagnostics.Process.Start(file);
}
As Ben Voigt pointed out DateTime
comparisons looks at the Ticks
property on a DateTime
DateTime.Ticks which is 1/10,000
of a millisecond.
Your loop probably doesn't execute that frequently.
Why wouldn't you just use a sleep or create a timer?
Use <
instead of !=
.
In any case, that's a terrible way of waiting for a specific time.
Is this an April Fools Joke?
Use <
instead of !=
.
I would switch that to being
while (System.DateTime.Now < new System.DateTime(2011, 05, 23, 22, 17, 0))
{
}
instead. Though I'd also be very wary of using a tight loop like that and would think that setting up a scheduled task or something would be much more effective.
Change it to user the less than operator. I am a little surprised it misses the time by seconds, bBut using equality for times is generally dangerous for this very precise reason (equality based on Ticks
).
System.DateTime.Now < otherDateTime
It should be noted that this type of "busy waiting" is incredibly wasteful of the processor. As others are pointing out, use a timer, or at least sleep the thread so that the processor can do something else with its time.
精彩评论