• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

Least resource hogging way to have a program 'sleep' until specified times

I normally use 'doevents' in a loop to have a program wait for a specified trigger but judging by the program's CPU usage while it's waiting, this isn't the best way to go.

I need to have a program trigger and record text on a webpage every 1/2 hour. I've thought about using Windows Scheduled TAsks to do this, but the lowest time denomination you can repeatedly run a program is once daily.

It'd be nice to have the program open and left that way while it waits every 1/2 hour to record text on the webpage without using as many CPUs as 'doevents' does. Any suggestions?
0
JohnDoeSr
Asked:
JohnDoeSr
2 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Add a Timer control to a Form (the Form Visible propery can be false if you don't want to see it):

' --------------------
'  Form1
' --------------------
Option Explicit

Private targetTime As Date

Private Sub Form_Load()
    ScheduleNextEvent 30
End Sub

Private Sub ScheduleNextEvent(ByVal delayInMinutes As Integer)
    targetTime = DateAdd("n", delayInMinutes, Now()) ' in XXX minutes from now...
    Timer1.Interval = 1000 ' check every second (change to suit your needs)
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    If Now() > targetTime Then
        Timer1.Enabled = False
       
       
        ' place your code to be executed here
        ' or make a call to another sub
       
       
        ScheduleNextEvent 30
    End If
End Sub
0
 
ADSaundersCommented:
Hi here's a skeleton extracted from a working NT service process I wrote for a shop floor data monitor. I've modded it slightly as I'm working on shorter intervals, and a process cycle of the order of 300 to 400 ms.
I've commented out the first def of const MajorInterval (30 mins) and replaced it with a shorter one (2 mins) for testing. just create a new project with an interval timer on the form named 'Timer', paste this code into the editor, and run it. watch the immediate window.

Regards .. Alan

Option Explicit
'Const defMajorInterval As Long = 1800000 ' 30 mins in ms
Const defMajorInterval As Long = 120000 ' 2 mins in ms
Const defMinorInterval As Long = 60000 ' Timer tick in ms (Max is 65535)
Const DaySecs          As Long = 86400      ' Number of seconds in a day
Dim ProcTime           As Long              ' The time for a single passthrough Timer process
Dim MajorInterval      As Long ' Time to next major interval in ms
Dim MinorInterval      As Long ' Time to next minor interval in ms

Private Sub Form_Load()
    MajorInterval = 0
    Timer_Timer ' Call Timer tick process explicitly with MajorInterval = 0
                ' This will carry out the first process, and set the timer for re-iteration
End Sub

Private Sub Timer_Timer()
    Timer.Enabled = False ' Don't want another timer tick whilst processing
    If MajorInterval = 0 Then ' Process first through, then after every MajorInterval
        ProcTime = CLng(Timer() * 1000)
        ' ********************
        '
        '  This is where you carry out your processing
        Debug.Print Now()
        '
        ' ********************
        ProcTime = (CLng(Timer() * 1000) - ProcTime)
        If ProcTime < 0 Then
            ProcTime = ProcTime + DaySecs ' Turned Midnight
        End If
    End If
    If MajorInterval = 0 Then ' MajorInterval expired, (or not started) so set Major tick
        MajorInterval = defMajorInterval
    End If
'    MajorInterval = MajorInterval
    If MajorInterval > defMinorInterval Then ' MajorInterval not expired,
                                             ' so set another full minor tick
        MinorInterval = defMinorInterval
        MajorInterval = MajorInterval - defMinorInterval
    Else ' remainder of MajorInteval less than default tick
        MinorInterval = MajorInterval
        MajorInterval = 0
    End If
    ' The following subtracts the processing time of this cycle from the next interval, so that
    ' overall intervals are more exact.
    ' Could run into problems if proctime regularly > defMinorInterval
    Timer.Interval = IIf(ProcTime > MinorInterval, 1, MinorInterval - ProcTime)
    Timer.Enabled = True
End Sub
0
 
JohnDoeSrAuthor Commented:
Awesome. Both answers are very informative. Will split the points.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now