Results 1 to 11 of 11

Thread: Timers

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Posts
    225
    Hiyas,

    Are there any alternatives other than a normal Timer?

    I want something really fast, something faster than one millisecond (or whatever the interval is measured in).

    Thanks,

    -Git

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Posts
    225
    Argh...

    I've written a game here which was running using a timer. It uses a time system - you can set the speed to seconds, minutes, hours or days.

    If you hit, for eg., days, it'll just add 1 to the D/M/Y display.

    However, I've just realised when events occur, if they're supposed to happen between one hour and another hour, they will really happen at the other hour, after it's supposed it.

    I thought about making the interval 0.01 milliseconds, and having it so it's always affecting all the time types, and then just have it so when you click the different settings (sec, min, hrs, days) it just makes the interval longer/shorter.

    But, I can't set it to under 1 in the timer interval.

    Does anyone have any ideas as to what I can do?

    Btw I would've posted this in the Games section but I figured it's more of a general VB topic...

    Thanks.

    -Git

  3. #3
    Guest
    you can use these API's to do it

    Code:
    Private Declare Function QueryPerformanceCounter Lib "kernel32" Alias "QueryPerformanceCounter" (lpPerformanceCount As LARGE_INTEGER) As Long
    Private Declare Function GetTickCount Lib "kernel32" Alias "GetTickCount" () As Long
    Private Type LARGE_INTEGER
        lowpart As Long
        highpart As Long
    End Type
    you can use either GetTickCount or QueryPerformanceCounter.
    GetTickCount gets to the millisecond.
    QueryPerformanceCounter gets it to the microsecond.

    to use QueryPerformanceCounter you need to do some simple math.


    Code:
    Dim liVariable As LARGE_INTEGER
    Dim dblResult As Double
    Call QueryPerformanceCounter(liVariable)
    dblResult = liVariable.lowpart + (liVariable.highpart * (2 ^ 32))
    the Return Value of QueryPerformanceCounter is a 64 bit number, so you need 2 32 bit numbers(Long) to make it 64, since the lowpart is 2 ^ 0 through 2 ^ 31 it can stay just where it is, but the highpart is the second 32 bits of the number which is 2 ^ 32 through 2 ^ 64 so you have to multiply the highpart by 2 ^ 32(which just happens to be 4294967296 but who wants to write that out all the time?) and add them(low part and high part) together, that gives you your 64bit number...
    why MS didnt just change the Return value to a Double, i dont know....

    I would write the code for the timer too, but I am so damn tired
    if anybody thinks I got this wrong, please correct me.... I am so damn tired.

    [Edited by denniswrenn on 08-20-2000 at 01:37 AM]

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Posts
    225
    Thanks for the quick reply.

    I'm not really sure how to use these commands though... how exactly would I go about implementing it? Anyone?

  5. #5
    Guest
    hehehe, that wasnt quick!
    that was a response to your first question
    it just took me that long to type it up, because I am so tired,


    I think you can store the first call to QueryPerfoetc in a variable, then continuously(with some kind of loop) compare that to the current time until the value ** milli/micro seconds apart.

    like I said I am very tired and can not even see straight right now... so I dont know if that will lock up VB if you try it.. or etc.....

  6. #6
    Guest
    Try this.
    Code:
    Private Declare Function GetTickCount Lib "kernel32" () As Long
    Dim bTime As Boolean
    
    Sub TimerEx(Interval As Long)
        
        Do Until bTime = False
            Start = GetTickCount
            
            Do While GetTickCount < Start + Interval
                DoEvents
            Loop
            
            '<--Place Code here->
            Print "Hello"
        Loop
            
    End Sub
    
    Private Sub Command1_Click()
        'Start the Timer
        bTime = True
        Call TimerEx(1000)
    End Sub
    
    Private Sub Command2_Click()
        'Exit the Timer
        bTime = False
    End Sub

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    HEre's how to use Querryperformance counter:
    Code:
    Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Currency) As Long
    Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Currency) As Long
    
    Dim Start as Currency, Finish as Currency, Freq as Currency
    
            QueryPerformanceCounter start
                'Your dealy
            QueryPerformanceCounter finish
            QueryPerformanceFrequency freq
            MsgBox CDbl(finish - start) / CDbl(freq) * 1000000
    It returns the result in milliseconds but with the decimal it's accurate up to 1 microsecond.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  8. #8
    Guest
    I didnt know you could change datatypes in API Calls!

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Of course you can, you can change pretty much everything, in the calls, but they have to match the arguments, Currency matches LARGE_INTEGER in the amount of memory.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10
    Guest
    cool

  11. #11
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    It doesn't always have to match the amount of memory, as long as you can track what memory it's going to put where, for example I often use the ScreenToClient API to change the coords of a RECT so it's relitive to a particular window

    Code:
    Private Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End Type
    Private Declare Function ScreenToClient Lib "user32" Alias "ScreenToClient" (ByVal hwnd As Long, lpPoint As Long) As Long 'Note lpPoint is a long
    
    Private Sub RectToClient(hWnd As Long, ChangeRect As RECT)
    
    Call ScreenToClient(hWnd,ChangeRect.Left)
    Call ScreenToClient(hWnd,ChangeRect.Right)
    
    End Sub
    The SCreen to client function takes a pointer to a pointAPI structure, but we change this to a pointer to a long then we pass it to the API, it thinks it's a pointer to a POINTAPI, so alters that long and the next long in memory, so we can do this for the first 2 longs (Left,Top) and the last to (Right,Bottm)

    No problem.

    ByVal parameters however must stick to the same amount of memory.


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