Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. check if 5 min have passed

check if 5 min have passed

Scheduled Pinned Locked Moved C#
question
12 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Yustme
    wrote on last edited by
    #1

    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!

    L 1 Reply Last reply
    0
    • Y Yustme

      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!

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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.


      Y 1 Reply Last reply
      0
      • L Luc Pattyn

        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.


        Y Offline
        Y Offline
        Yustme
        wrote on last edited by
        #3

        I know how... But obviously, I'm doing something wrong. So I thought another developer could help me out here...

        L 1 Reply Last reply
        0
        • Y Yustme

          I know how... But obviously, I'm doing something wrong. So I thought another developer could help me out here...

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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.


          Y 1 Reply Last reply
          0
          • L Luc Pattyn

            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.


            Y Offline
            Y Offline
            Yustme
            wrote on last edited by
            #5

            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.

            L S 2 Replies Last reply
            0
            • Y Yustme

              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.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              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.


              Y 1 Reply Last reply
              0
              • L Luc Pattyn

                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.


                Y Offline
                Y Offline
                Yustme
                wrote on last edited by
                #7

                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.

                L 1 Reply Last reply
                0
                • Y Yustme

                  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.

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  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.


                  Y 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    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.


                    Y Offline
                    Y Offline
                    Yustme
                    wrote on last edited by
                    #9

                    [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!

                    L 1 Reply Last reply
                    0
                    • Y Yustme

                      [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!

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      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.


                      L 1 Reply Last reply
                      0
                      • L Luc Pattyn

                        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.


                        L Offline
                        L Offline
                        llandyw
                        wrote on last edited by
                        #11

                        Why not just use a timer? Set one up so it triggers in 600 seconds, and use the event to set a flag? Andy

                        1 Reply Last reply
                        0
                        • Y Yustme

                          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.

                          S Offline
                          S Offline
                          Som Shekhar
                          wrote on last edited by
                          #12

                          Your code will never calculate what you want here. Only for the first time this command is run, the lastTimeCommandWasUsed will be equal to DateTime.MaxValue. hence allowUse is set to true and lastTimeCommandWasUsed is assigned to currentTime. Now, when you run this command for the second time, first condition: if (lastTimeCommandWasUsed == DateTime.MaxValue) is false. Hence allowUse 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"

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups