Hi,
How do I make a program do something at a specific time, say 6:00 pm. one wya I see is to take the current time, converts it to milliseconds, takes the specific time also in ms. Finds difference. Then use sleep(). Any better ideas?
Thomas :U
Try SetTimer/KillTimer for setting a running timer (in milliseconds.) Seems to be pretty accurate once started and uses very little system resources. On the down side, the thread must remain running as it counts down.
To cause something to happen at a specific time, how about GetCurrentTime? Then compare that result to whatever your set time is. You could even create a second timer to check every second to see if GetTime has arrived. Maybe you could expand on this very experimental code:
.data
szStopped db 'MyApp v0.01',0,0
szRunning db 'MyApp Time Remaining: ',0,0,0,0,0,0
szTime dq 0
iTitleUpd dd 0
szTitleUpd dq 0
.code
.elseif eax==WM_HOTKEY ; when our hotkey is pressed
invoke UnregisterHotKey,hWnd,1100 ; needed to prevent looping!
invoke GetDlgItemText,hWnd,IDC_EDT1,addr szTime,4 ; limit 4 chars
invoke atodw,addr szTime ; ascii to dword
mov [iTitleUpd],eax ; title timer value (single seconds)
mov ecx,1000
mul ecx ; multiply eax by 1000 to get milliseconds
invoke SetTimer,hWnd,111,eax,0 ; primary timer
invoke SetTimer,hWnd,113,1000,0 ; title-update timer, 1 second
invoke SetWindowText,hWnd,addr szRunning
mov [active],1 ; timer active flag
.elseif eax==WM_TIMER ; timers expired
mov eax,wParam
.if eax==111 ; primary timer
invoke Beep,500,1000 ; hey we're done!
invoke KillTimer,hWnd,111
invoke RegisterHotKey,hWnd,1100,hVKradicals,hVKey
invoke SetWindowText,hWnd,addr szStopped
mov [active],0 ; flag as done
.elseif eax==113 ; "update title" timer
dec [iTitleUpd] ; decrement counter
invoke dwtoa,[iTitleUpd],addr szTitleUpd
mov ecx,offset szRunning ; pointer as ecx
mov edx,offset szTitleUpd
add ecx,23 ; go to first null
call lpszCPY ; copy number
mov word ptr [ecx],0073h ; output "s" and null
.if [iTitleUpd]>0 ; while above zero sec remain
invoke SetWindowText,hWnd,addr szRunning
invoke SetTimer,hWnd,113,1000,0 ; restart timer
.else
invoke SetWindowText,hWnd,addr szStopped
invoke KillTimer,hWnd,113
.endif
.endif
'lpszCPY' is part of a slew of simple string-and-byte functions I'm building.
lpszCPY proc ; copies edx string to ecx string
@@:
mov al, byte ptr [edx] ; input offset as edx
mov byte ptr [ecx],al ; copy byte
inc edx
inc ecx
test al,al ; was it a null?
jnz @B
dec ecx ; land on nulls, not after
dec edx
ret
lpszCPY endp
Thomas,
Here is an alarm clock program. Originally coded by Roman Nowicki and rewritten using Radasm by Ranma_at.
Paul
[attachment deleted by admin]
Hi,
Thanx Mark, Thanx Paul. Those examples are very good. Thanx again
Thomas :U