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
Printable View
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
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
you can use these API's to do it
you can use either GetTickCount or QueryPerformanceCounter.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
GetTickCount gets to the millisecond.
QueryPerformanceCounter gets it to the microsecond.
to use QueryPerformanceCounter you need to do some simple math.
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...Code:Dim liVariable As LARGE_INTEGER
Dim dblResult As Double
Call QueryPerformanceCounter(liVariable)
dblResult = liVariable.lowpart + (liVariable.highpart * (2 ^ 32))
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]
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?
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.....
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
HEre's how to use Querryperformance counter:
It returns the result in milliseconds but with the decimal it's accurate up to 1 microsecond.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
I didnt know you could change datatypes in API Calls!
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.
cool :cool:
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
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)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
No problem.
ByVal parameters however must stick to the same amount of memory.