Announcement

Collapse
No announcement yet.

Sleep for long time

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Sleep for long time

    I am working on an console application that requires it to sleep for long periods of time between processing. Sometimes for only a few minutess, other times for hours.

    Currently, I am using the below routine to sleep for those time periods...
    Code:
     
    SUB SleepMinutes(lngMinutes AS LONG)
     SLEEP 60000 * lngMinutes
    END SUB
    I understand this is a console application and this is the Windows section but was curious just in general windows programming is this the most approriate way to do a long-term sleep?
    Last edited by George Bleck; 15 Feb 2008, 09:49 AM.
    <b>George W. Bleck</b>
    <img src='http://www.blecktech.com/myemail.gif'>

  • #2
    Long Term Sleep Cycle

    Hi George;

    While what your doing will work, it might be beneficial to:

    i = 1
    DO UNTIL i = lngMinutes
    SLEEP 60000
    'Display a message here or send a message to a remote computer or
    'device.
    INCR i
    LOOP

    This way the user gets a periodic indication that the app is still operating.

    Comment


    • #3
      I had the same question some time ago.

      http://www.powerbasic.com/support/pb...highlight=cool

      My solution is at: http://www.powerbasic.com/support/pb...ad.php?t=24962

      Maybe food for thought.
      There are no atheists in a fox hole or the morning of a math test.
      If my flag offends you, I'll help you pack.

      Comment


      • #4
        > Sometimes for only a few minutess, other times for hours.

        Length of nap time is determined by....????

        If there is some event which says, "Hey, wake up and do something now!" then perhaps a Windows event object is what you should be using. Very, very efficient.

        I think I posted a demo on using Windows' event objects. Try a search on 'event' in the 'mesage title' for the source code forum.

        MCM
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          Originally posted by Eric Pearson
          ...if you put a program to sleep for that long it will not respond properly to a Close command...
          According to Eric's statement in the thread you kindly linked this is one of the things I would need to consider, an app hanging up the system if it needs to reboot.

          My application is unmonitored and the machine reboots at midnight every night so this would not be good.

          So this is what I think I'm going to go with as it is simple, the app awakens once per second so will respond to close in a reasonable time frame. Point point accuracy is not relevant in my app so even being off by a few minutes is not important.

          Code:
           
          SUB SleepMinutes( lngMinutes AS LONG )
           DIM lngSeconds AS LONG
           FOR lngSeconds = 1 TO (60 * lngMinutes)
            SLEEP 1000
           NEXT lngSeconds
          END SUB
          <b>George W. Bleck</b>
          <img src='http://www.blecktech.com/myemail.gif'>

          Comment


          • #6
            So this is what I think I'm going to go with as it is simple, the app awakens once per second so will respond to close in a reasonable time frame.
            You must get a kickback on hardware purchases, since that code will eat clocks.

            Let me guess what is not shown... inside the loop you check a condition, and if true, you do something; otherwise you do nothing?

            As I said, if you want to check a condition which may occur any time at irregular intervals, Windows Event Objects is where you should be looking.

            FOR WHAT ARE YOU CHECKING? Let's make this code something you can be proud of, not something requiring you to put a bag over your head when you show it to your friends and colleagues.

            MCM
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              No reason for pate when tuna will do :laugh:

              The specifics of the code are proprietary but in a nutshell

              Code:
               
              Do
               Wakeup
               Read Ini for for next sleep cycle duration and put into X
               Process Data present at that time
               Write report
               Sleep X minutes
              Loop
              The amount of data present during each cycle is completely variable even on a set cycle. What my client wants is to change the granularity of how often the report runs. They sometimes want it every 15 mins, 30 mins, 1 hours, sometimes every 5 hours, who knows. They make the change when they need to change the granularity of time. It can run days or weeks on the same cycle then they change it for a short time to suit their current need.

              Simply put, all I need is a timer that will wait X minutes until the next cycle needs to run; nothing more. But, I do need the application to gracefully close if the machine is rebooted. Anything more complex then that is a waste. The application has no UI and the device it runs on has no screen/kb/etc. All changes are done via the INI file.
              Last edited by George Bleck; 15 Feb 2008, 03:36 PM.
              <b>George W. Bleck</b>
              <img src='http://www.blecktech.com/myemail.gif'>

              Comment


              • #8
                checking the time

                From the win32 api help:

                An application written for Microsoft® Windows® creates a timer by using the SetTimer function. If you specify a window handle in the call to SetTimer, the application associates the timer with that window. Whenever the time-out value for the timer elapses, the system posts a WM_TIMER message to the window associated with the timer. If no window handle is given in the call to SetTimer, the application that created the timer must monitor its message queue for WM_TIMER messages and dispatch them to the appropriate window.
                A new timer starts timing the interval as soon as it is created. An application can change a timer's time-out value by using SetTimer and can destroy a timer by using the KillTimer function. To use system resources efficiently, applications should destroy timers that are no longer necessary.
                Each timer has a unique identifier. When creating a timer, an application can either specify an identifier or have the system create a unique value. The first parameter of a WM_TIMER message contains the identifier of the timer that posted the message.
                JS
                John, Hypnotherapist Extraordinaire, NLP Trainer,
                and Full Time Grad Student in Clinical Psychology
                ---------------------------------------------------
                John Strasser
                Phone: 480 - 344 - 7713
                http://www.inthespiritmarketing.com
                http://www.lifetranscendent.com
                John,
                --------------------------------
                John Strasser
                Phone: 480 - 273 - 8798

                Comment


                • #9
                  That's certainly an interesting application..

                  I guess editing an ini file must be easiest for them; I think I would have probably used Windows' Scheduler to
                  Code:
                  processreportdata.exe [optional parameters]
                  ...and just changed the frequency when I changed my mind about it.

                  Didn't mean to be so abrupt.. . I think it's a leftover from yesterday..

                  I was at a client site* and he insisted I watch some SNL 'Nick Burns the Computer Guy' sketches (I'd never seen one before) and I'm afraid I might have caught something from that. ("MOVE!!")

                  MCM
                  *Plainville MA if you must know. I leave Wisconsin weather and what do I get? You got it: snow, ice and cold. My only consolation was, six inches of snow and a temperature of plus 10 was considered almost a natural disaster. Sheesh, New Englanders are such wimps.
                  Michael Mattias
                  Tal Systems (retired)
                  Port Washington WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment


                  • #10
                    The PC itself is in a hazardous area so people do not visit it unless there is a problem.

                    Yes they can remote into it but there is little need for a GUI interface for anything. Literally all they want the application to do is process the relevant data on a specific but changable schedule.

                    As it is a console application and has no message pump by default so I did not want to go the route of a WM_TIMER. Not that I cant rewrite the application to use one but sounds like hammer to drive a screw, it'll work but might be a bit overkill.

                    If I use the windows scheduler I cannot easily change the time via the INI file (I over simplied it a bit as they will be changing some other parameters when they change the time, and the changes themselves are handled by another application on a remote pc, not a human)

                    This seems almost, hard. From what people are saying you cannot code a simple, but efficient long term pause in-line?
                    Last edited by George Bleck; 16 Feb 2008, 04:39 PM.
                    <b>George W. Bleck</b>
                    <img src='http://www.blecktech.com/myemail.gif'>

                    Comment


                    • #11
                      >you cannot code a simple, but efficient long term pause in-line?

                      Sure you can.....

                      Waitable Timer Object Demo June 2005
                      Michael Mattias
                      Tal Systems (retired)
                      Port Washington WI USA
                      [email protected]
                      http://www.talsystems.com

                      Comment


                      • #12
                        >you cannot code a simple, but efficient long term pause in-line

                        'SLEEP bignumber' will work, too. (Albeit at the cost of panache points).

                        What you want to avoid is looping to check the time.

                        If you know HOW LONG you want to wait, then SLEEP enough.

                        If you know WHEN you want to wake up, convert that to 'how long' and SLEEP as above, don't loop to check the time.

                        What I'm trying to figure out is how the user is supposed to maintain this INI file while this program is trying to read it. If the user deletes or adds a record, which record does this program use next time it reads the file?

                        MCM
                        Michael Mattias
                        Tal Systems (retired)
                        Port Washington WI USA
                        [email protected]
                        http://www.talsystems.com

                        Comment


                        • #13
                          If you read the original psuedocode it reads the INI file on wakeup.

                          The INI file is accessed via the windows ReadPrivateProfileString and WritePrivateProfileString when it needs to.

                          I think a minor issue is you are reading a little to far into the need Mike. The data crunching is set in stone, it's a 3rd party DLL. All I need to do is set up a simple cycler to process the data. I understand there are a million ways to do the same thing, sometimes the easiest is, well, the easiest. The program need not be complicated more then the psuedocode.

                          If this was a GUI application, or something where the client would be changing the INI file often or the client needed constant update of its status I could understand the additions. I was simply looking for a way to create a long term pause. I'm not trying to be coy as I REALLY do appreciate the help, but I don't need assistance on the basic program rules, just the one aspect. I come here because I trust the opinions of the programers, especially you Mike as I can appreciate your ethic.

                          Sometimes building a log cabin with a fireplace is overkill when all you really needed was wood for a campfire :laugh:
                          Last edited by George Bleck; 21 Feb 2008, 10:43 AM.
                          <b>George W. Bleck</b>
                          <img src='http://www.blecktech.com/myemail.gif'>

                          Comment

                          Working...
                          X