check if 5 min have passed
-
Hi, How can I check if 5 minutes have passed with the datetime object? I've tried a few things, but none of them worked for me. Thanks in advance!
How do you do it as a human being, without computer? Substitute wrist watch for System.DateTime.Now :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
How do you do it as a human being, without computer? Substitute wrist watch for System.DateTime.Now :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
I know how... But obviously, I'm doing something wrong. So I thought another developer could help me out here...
If you know how to do it, and have tried to get something working, but failed, then tell us about it, describe the concept and implementation, give precise symptoms, and show code if and as needed. Such are the rules of these forums. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
If you know how to do it, and have tried to get something working, but failed, then tell us about it, describe the concept and implementation, give precise symptoms, and show code if and as needed. Such are the rules of these forums. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
I pass 5 min to this function:
DateTime lastTimeCommandWasUsed = DateTime.MaxValue;
private bool AtLeastXMinutesHavePassed(int xMinutes) { // Check if "x" minutes have elapsed DateTime currentTime = DateTime.Now; bool allowUse = false; if (lastTimeCommandWasUsed == DateTime.MaxValue) { allowUse = true; } else { TimeSpan elapsedTime = currentTime - lastTimeCommandWasUsed; allowUse = elapsedTime.TotalMinutes > xMinutes; } // Return status and keep track of new last used time if (allowUse) { lastTimeCommandWasUsed = currentTime; return true; } return false; }
And it returns very fast 'false'. After debugging it, it seems to check for seconds instead of minutes. Can't figure out what is going wrong.
-
I pass 5 min to this function:
DateTime lastTimeCommandWasUsed = DateTime.MaxValue;
private bool AtLeastXMinutesHavePassed(int xMinutes) { // Check if "x" minutes have elapsed DateTime currentTime = DateTime.Now; bool allowUse = false; if (lastTimeCommandWasUsed == DateTime.MaxValue) { allowUse = true; } else { TimeSpan elapsedTime = currentTime - lastTimeCommandWasUsed; allowUse = elapsedTime.TotalMinutes > xMinutes; } // Return status and keep track of new last used time if (allowUse) { lastTimeCommandWasUsed = currentTime; return true; } return false; }
And it returns very fast 'false'. After debugging it, it seems to check for seconds instead of minutes. Can't figure out what is going wrong.
Yustme wrote:
it returns very fast
of course it does, there are no loops, no delays, it is just a few statements in a straight line, so it returns "immediately".
Yustme wrote:
And it returns ... 'false'
I doubt that.
Yustme wrote:
it seems to check for seconds
No way. I suggest you add logging statements to output the value of relevant variables. Example:
Console.WriteLine("now="+currentTime);
Console.WriteLine("last="+lastTimeCommandWasUsed );FWIW: unrelated to potential problems, your code would be somewhat simpler if you initialized using MinValue instead of MaxValue! :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
Yustme wrote:
it returns very fast
of course it does, there are no loops, no delays, it is just a few statements in a straight line, so it returns "immediately".
Yustme wrote:
And it returns ... 'false'
I doubt that.
Yustme wrote:
it seems to check for seconds
No way. I suggest you add logging statements to output the value of relevant variables. Example:
Console.WriteLine("now="+currentTime);
Console.WriteLine("last="+lastTimeCommandWasUsed );FWIW: unrelated to potential problems, your code would be somewhat simpler if you initialized using MinValue instead of MaxValue! :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
I think you miss understood my 'very fast'. Instead of 5 min, 5 sec. this is the loop, which is not in that function:
private void RestartAfterXMin() { while (this.AtLeastXMinutesHavePassed(5)) { Thread.Sleep(1000); if (this.stopRequested) return; } this.AutoStop(); Application.Restart(); }
It's initialized with minvalue, nothing changed. -
I think you miss understood my 'very fast'. Instead of 5 min, 5 sec. this is the loop, which is not in that function:
private void RestartAfterXMin() { while (this.AtLeastXMinutesHavePassed(5)) { Thread.Sleep(1000); if (this.stopRequested) return; } this.AutoStop(); Application.Restart(); }
It's initialized with minvalue, nothing changed.I suggested to add logging statements and debug. And I told you MinValue was unrelated to the bug, but would allow for simpler code. Now solve your problem, don't wait to be spoon fed. :|
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
I suggested to add logging statements and debug. And I told you MinValue was unrelated to the bug, but would allow for simpler code. Now solve your problem, don't wait to be spoon fed. :|
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[quote] I suggested to add logging statements and debug. [/quote] How different would that be when I used the debugger? [quote] And I told you MinValue was unrelated to the bug, but would allow for simpler code. [/quote] I still expected that something would change, not that it would solve the bug. [quote] Now solve your problem, don't wait to be spoon fed. [/quote] I could say that programming isn't like boxing, where you tell me a few words like 'you can take him down, you're stronger' while i know i'll get my ass wooped by this guy called Muhammed Ali (hypothetically, i'm fighting him in the ring).. Instead, you know, a few posts back you pointed me out that there is a quideline for asking questions. Did you know there is one for 'answering questions' too? A small quote from that guideline: [quote] Insults, slap-downs and sarcasm aren't welcome. Let's work to help developers, not make them feel stupid.. [/quote] I'm not asking someone to do my work, pointing me in the right direction is more the enough!
-
[quote] I suggested to add logging statements and debug. [/quote] How different would that be when I used the debugger? [quote] And I told you MinValue was unrelated to the bug, but would allow for simpler code. [/quote] I still expected that something would change, not that it would solve the bug. [quote] Now solve your problem, don't wait to be spoon fed. [/quote] I could say that programming isn't like boxing, where you tell me a few words like 'you can take him down, you're stronger' while i know i'll get my ass wooped by this guy called Muhammed Ali (hypothetically, i'm fighting him in the ring).. Instead, you know, a few posts back you pointed me out that there is a quideline for asking questions. Did you know there is one for 'answering questions' too? A small quote from that guideline: [quote] Insults, slap-downs and sarcasm aren't welcome. Let's work to help developers, not make them feel stupid.. [/quote] I'm not asking someone to do my work, pointing me in the right direction is more the enough!
Yustme wrote:
How different would that be when I used the debugger?
Logging leaves a trail which makes it easier to spot a pattern, it shows the results automatically for each consecutive run, and it takes virtually no time, whereas using the debugger requires manual intervention and costs seconds per operation, which in a time-related problem often isn't very wise.
Yustme wrote:
pointing me in the right direction is more the enough
that is what you got. :|
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
Yustme wrote:
How different would that be when I used the debugger?
Logging leaves a trail which makes it easier to spot a pattern, it shows the results automatically for each consecutive run, and it takes virtually no time, whereas using the debugger requires manual intervention and costs seconds per operation, which in a time-related problem often isn't very wise.
Yustme wrote:
pointing me in the right direction is more the enough
that is what you got. :|
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
I pass 5 min to this function:
DateTime lastTimeCommandWasUsed = DateTime.MaxValue;
private bool AtLeastXMinutesHavePassed(int xMinutes) { // Check if "x" minutes have elapsed DateTime currentTime = DateTime.Now; bool allowUse = false; if (lastTimeCommandWasUsed == DateTime.MaxValue) { allowUse = true; } else { TimeSpan elapsedTime = currentTime - lastTimeCommandWasUsed; allowUse = elapsedTime.TotalMinutes > xMinutes; } // Return status and keep track of new last used time if (allowUse) { lastTimeCommandWasUsed = currentTime; return true; } return false; }
And it returns very fast 'false'. After debugging it, it seems to check for seconds instead of minutes. Can't figure out what is going wrong.
Your code will never calculate what you want here. Only for the first time this command is run, the
lastTimeCommandWasUsed
will be equal toDateTime.MaxValue
. henceallowUse
is set to true andlastTimeCommandWasUsed
is assigned tocurrentTime
. Now, when you run this command for the second time, first condition:if (lastTimeCommandWasUsed == DateTime.MaxValue)
is false. HenceallowUse
is false still. This the second condition is false too. Hence you get false back. there is no place where you are returning the value that you seek. just make one change here. instead of returning false, return "allowUse"