Re: Put thread in sleep mode
On Sat, 11 Aug 2007 02:12:29 +0100, MiG <miguel.a.guedes@gmail.com> wrote:
Apart from Sleep, what other API functions are there that allow for
programmatically scheduling thread execution time?
Look into multi-media timers. You should be able to have a "TimeProc" callback
called periodically with 1 ms accuracy. It'll be tough to get 60 Hz because 60
doesn't divide 1000.
This no-frills, hastily written tidbit runs a beeper ... TimeProc called every
20 ms (50 Hz) ... short beep every second ... long beep once a minute. After
about 30 minutes it does not appear to have drifted (by comparison to an analog
sweep-second wall clock). [There's no built-in way to stop it ... kill it with
TaskMgr.]
#include <windows.h>
#define WM_TICKER_SECOND WM_APP
#define WM_TICKER_MINUTE (WM_APP + 1)
void CALLBACK TimeProc(UINT uID, UINT uMsg, DWORD dwUser,
DWORD dw1, DWORD dw2)
{
DWORD t = timeGetTime(),
m = t%60000,
s = t%1000;
if ( m < 20 )
PostMessage((HWND) dwUser, WM_TICKER_MINUTE, 0, 0);
else if ( s < 20 )
PostMessage((HWND) dwUser, WM_TICKER_SECOND, 0, 0);
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg,
WPARAM wParam, LPARAM lParam)
{
if ( uMsg == WM_TICKER_SECOND )
Beep(440,100);
else if ( uMsg == WM_TICKER_MINUTE )
Beep(440,500);
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
TIMECAPS tc;
timeGetDevCaps(&tc, sizeof(TIMECAPS));
timeBeginPeriod(tc.wPeriodMin);
WNDCLASS WndClass = {0, WindowProc, 0, 0, hInstance,
NULL, NULL, NULL, NULL, "HZ50_CLASS"};
RegisterClass(&WndClass);
HWND hwnd = CreateWindow("HZ50_CLASS", "HZ50_WINDOW", 0, 0, 0,
0, 0, NULL, NULL, hInstance, 0);
timeSetEvent(20, 0, TimeProc, (DWORD_PTR) hwnd, TIME_PERIODIC);
while( GetMessage( &msg, NULL, 0, 0 ) )
DispatchMessage(&msg);
return msg.wParam;
}
--
- Vince