Results 1 to 5 of 5

Thread: set time period to execute a command in VB

Hybrid View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2000
    Location
    Posts
    124

    Question

    Is there a better way to automatically execute a procedure as the time specified by the application user ? I was thinking of using the timer to check the system time and compare to the time set by the user (my project will try to give the user to set instead of hard coded) to execute......I know it sounds silly...........

  2. #2
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736

    That's how I do it

    I have a textbox that the user keys in the time to start the process. I use a timer to check the system time and when it matches the time in the textbox, the process begins.

    Code:
    Private Sub Timer1_Timer()
        Dim dTmp As Date
        On Error Resume Next
        Timer1.Enabled = False
        
        If Trim$(txtTime.Text) <> "" Then
            dTmp = CDate(txtTime.Text)
            If Format(Now, "HH:NN:SS AMPM") = Format(dTmp, "HH:NN:SS AMPM") Then
                ' Do your stuff here
            End If
        End If
        Timer1.Enabled = True
    
    End Sub

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2000
    Location
    Posts
    124
    Does this mean the program has to keep looking at the system time .......? Will takes up the system resource ? I was wondenring is there a function in the system that will wakes up this .........

  4. #4
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Depends on what you are doing but this is a pause function
    Code:
    'pause for a 5 second interval
    
    Private Declare Function GetTickCount Lib "kernel32" Alias "GetTickCount" () As Long
    
    Private Sub Pause(Interval As Long)
        Start = GetTickCount
        Do While GetTickCount < Start + Interval
            DoEvents
        Loop
    End Sub
    
    
    'event code
    Pause 5000    'Pause for 5 seconds
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  5. #5
    Addicted Member Shrog's Avatar
    Join Date
    Aug 1999
    Location
    Darkest Africa
    Posts
    186

    Smile

    The pause is fine for most things, but a little heavy for time critical situations (notice how the CPU time shoots up to max when you use Doevents like that). In time critical situations, the Sleep API can replace the DoEvents.

    As far as scheduling a command for a given time, the Timer control as described by jbart is not all that heavy on resources. If you are just checking the system time every second, or even every half second, it will not make a difference. In fact, you dont have to Disable and Enable the timer each time.

    If you are going to do LOTS of processing every tenth of a second, then it might be a bit heavy.

    Shrog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width