Valhalla Legends Forums Archive | Battle.net Bot Development | Most effective "Sleep" function

AuthorMessageTime
phvckmeh
[code]
Public Declare Function GetTickCount Lib "kernel32.dll" () As Long

Public Sub Sleep(Millaseconds As Long)

Dim EndTime As Long
EndTime = GetTickCount + Millaseconds

Do Until GetTickCount >= EndTime
DoEvents
Loop

End Sub
[/code]

I wrote this myself and im wondering if this is the "safest" and more effective way to make my bot sleep, for example if i did something like

[code]
Send "/join the void"
Sleep(2000)
Send "/join " & strLastChannel
[/code]

August 17, 2004, 8:18 AM
Gangz
What is this accomplishing?
August 17, 2004, 8:39 AM
OnlyMeat
[quote author=phvckmeh link=board=17;threadid=8197;start=0#msg75910 date=1092730710]
[code]
Public Declare Function GetTickCount Lib "kernel32.dll" () As Long

Public Sub Sleep(Millaseconds As Long)

Dim EndTime As Long
EndTime = GetTickCount + Millaseconds

Do Until GetTickCount >= EndTime
DoEvents
Loop

End Sub
[/code]

I wrote this myself and im wondering if this is the "safest" and more effective way to make my bot sleep, for example if i did something like

[code]
Send "/join the void"
Sleep(2000)
Send "/join " & strLastChannel
[/code]

[/quote]

Thats very poor im sorry to say that will eat processor cycles, i suggest using a timer for doing that.

That way the system will generate a timer event after 2 seconds and wont waste valuable processor cycles in a pointless loop.

Also the most efficient sleep function is the raw win32 api:-
[code]
VOID Sleep(
DWORD dwMilliseconds // sleep time in milliseconds
);
[/code]

Or the more advanced:-
[code]
DWORD SleepEx(
DWORD dwMilliseconds, // time-out interval in milliseconds
BOOL bAlertable // early completion flag
);
[/code]

The calling thread will enter an efficient wait state using those.

The first api function can be applied to vb quite easily using the standard declare statement which gives you access to the win32 api.

An additional note also those functions will block your main thread in vb for the specified time you pass into the function which is why i again recommend using a timer for doing this:-

[code]
Send "/join the void"
Sleep(2000)
Send "/join " & strLastChannel
[/code]
August 17, 2004, 8:54 AM
Kp
If you want to efficiently sleep, but also track when window messages come in, consider using one of the MsgWaitForMultipleObjects family. It'll awaken when the time elapses, a message is delivered, or when one of the watched handles becomes signalled.
August 17, 2004, 4:20 PM

Search