CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 1999
    Location
    Montreal, Quebec, Canada
    Posts
    192

    Create a timer at runtime

    I'm trying to create my own timer class to allow more than 64 seconds.

    But I'm trying to do it without Sleep or something similar, so the thing I'm trying to do is to use the normal timer and run it a few times.

    so I'm trying to do:


    Dim withevents Timer1 as Timer




    Works fine, but how the hell do I instanciate this thing? I mean, CreateObject("VB.Timer") doesn't work of course.

    Any idea? Now I'm switching to datediff otherwise!


  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Create a timer at runtime

    Here is an API timer (without timer)

    The SetTimer function creates a timer with the specified time-out value.

    'In a module
    '-----------
    Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    '· uElapse - Specifies the time-out value, in milliseconds.

    Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
    Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer


    Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
    debug.print "Time interval"
    KillTimer Form1.hwnd, 0
    End Sub

    'In a form
    '--------
    Private Sub Form_Load()
    'Create an API-timer 1000 msec
    SetTimer Me.hwnd, 0, 1000, AddressOf TimerProc
    End Sub

    Private Sub Form_Unload(Cancel As Integer)
    'Kill our API-timer
    KillTimer Me.hwnd, 0
    End Sub




    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  3. #3
    Join Date
    Jul 1999
    Location
    Montreal, Quebec, Canada
    Posts
    192

    Re: Create a timer at runtime

    This is excellent, I didn't know there was api timer.. otherwise I wouldn't have wasted my time.

    Is there a possible problem of enabling and disabling the timer in the callback function itself?

    I mean, I don't want the timer to work when I'm in the callback function, and I wish to start it again when I'm getting out of it.

    Thanks!


  4. #4
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Create a timer at runtime

    I never tried this, but in the code you can see how to enable/disable timer from the main proc.
    Good luck

    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

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

Featured