tourach
asked on
Timer Routine
Where can I find, or what is the code for a routine that
will allow a user to set a timer, and then receive notification that that timer has 'gone off' (possibly incorporating pthreads)
-On a Unix Alpha machine
will allow a user to set a timer, and then receive notification that that timer has 'gone off' (possibly incorporating pthreads)
-On a Unix Alpha machine
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Edited text of question
ASKER
On a Unix Alpha Machine
break the files by searching for this line...
/* Filename: Timers.c */
#if 0
ABSTRACT START
TITLE
Windows Timer Sample Application
EXE
timers.exe
AUTHOR
Bob Gunderson - Microsoft Corp.
CONTENT
This sample application demonstrates how to create and terminate
application timers and how to change the rate of an existing timer.
On menu commands, it will create a timer that can either dispatch a
WM_TIMER message to a window procedure or call a callback function.
The timer can be set to run at one of three speeds.
The sample also demonstrates calling the Toolhelp.dll timer services.
TAG
1.3.2
2.1
2.2
3.1.1.21
3.1.15
ABSTRACT END
#endif
#include "windows.h"
#include "timers.h"
#include "toolhelp.h"
//------------------------
// Global Variables...
//------------------------
HANDLE hInstance; // Global instance handle for application
int TimerType = cmdStop; // Current timer type
int TimerSpeed = cmdSlow; // Current timer speed
int hwndKill, idKill; // Values to use on KillTimer
int Millisec = 5000; // Current timer speed in milliseconds
FARPROC fpTimerCallback; // MakeProcInstance of callback
HWND hwndMain; // Main hwnd. Needed in callback
int counter = 0; // Counter to display when timer goes off
char szText[80]; // Temp storage to build strings
TIMERINFO ti; // Structure to pass to Toolhelp.dll
//------------------------
// Function declarations
//------------------------
LONG FAR PASCAL __export TimerTestWndProc(HWND, UINT, WPARAM, LPARAM);
void StartTimer(HWND, int, int);
WORD FAR PASCAL __export TimerCallbackProc(HWND, WORD, int, DWORD);
//------------------------
// WinMain
//------------------------
int PASCAL WinMain(HANDLE hInst, HANDLE hInstPrev, LPSTR lpstrCmdLine, int cmdShow)
{
MSG msgMain;
WNDCLASS wc;
// Set the global instance variable
hInstance = hInst;
// Register the window class if this is the first instance.
if (hInstPrev == NULL)
{
wc.lpszMenuName = "TimerMenu";
wc.lpszClassName = "TimerTestApp";
wc.hInstance = hInst;
wc.hIcon = LoadIcon(hInst, "TimerIcon");
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = COLOR_WINDOW + 1;
wc.style = 0;
wc.lpfnWndProc = TimerTestWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
if (!RegisterClass(&wc))
return(0);
}
// Create the main window
if ((hwndMain = CreateWindow("TimerTestApp
"Sample Timer Application",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInst, NULL)) == NULL)
return(0);
// Need to call MakeProcInstance because multiple instances of this
// application can exist.
fpTimerCallback = MakeProcInstance(TimerCall
// Show the window and make sure it is updated.
ShowWindow(hwndMain, cmdShow);
UpdateWindow(hwndMain);
// Main message "pump"
while (GetMessage((LPMSG) &msgMain, NULL, 0, 0))
{
TranslateMessage((LPMSG) &msgMain);
DispatchMessage((LPMSG) &msgMain);
}
// Even though Windows destroys timers created by this app, destroying
// the things the application creates is good practice.
if (TimerType != cmdStop)
KillTimer(hwndKill, idKill);
return(0);
}
//------------------------
// TimerTestWndProc
//
// Window procedure for the sample applications window.
//
//------------------------
LONG FAR PASCAL __export TimerTestWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HMENU hMenu;
HDC hdc;
switch(msg) {
//
// Handle menu selections
//
case WM_COMMAND:
switch (wParam) {
case cmdStop:
case cmdToHwnd:
case cmdCallProc:
//
// Change the checkmark to the proper item
//
hMenu = GetMenu(hwnd);
CheckMenuItem(hMenu, TimerType, MF_UNCHECKED | MF_BYCOMMAND);
CheckMenuItem(hMenu, wParam, MF_CHECKED | MF_BYCOMMAND);
//
// Kill any existing timer. Also clear the text displayed
// by a previous timer going off.
//
KillTimer(hwndKill, idKill);
idKill = 0;
counter = 0;
InvalidateRect(hwnd, NULL, TRUE);
UpdateWindow(hwnd);
//
// Set the new timer type and start it up.
//
TimerType = wParam;
if (wParam != cmdStop)
StartTimer(hwnd, TimerType, TimerSpeed);
break;
//
// Set the timer speed
//
case cmdFast:
Millisec = 250;
goto Common;
case cmdMedium:
Millisec = 1000;
goto Common;
case cmdSlow:
Millisec = 5000;
Common:
//
// Change the checkmark to the proper item
//
hMenu = GetMenu(hwnd);
CheckMenuItem(hMenu, TimerSpeed, MF_UNCHECKED | MF_BYCOMMAND);
CheckMenuItem(hMenu, wParam, MF_CHECKED | MF_BYCOMMAND);
//
// Reset the timer. No need to kill the existing one and
// start a new one. Windows resets an existing timer
// with a new speed if the hWnd and ID are identical to
// an existing timer.
//
TimerSpeed = wParam;
if (TimerType != cmdStop)
StartTimer(hwnd, TimerType, TimerSpeed);
break;
}
break;
case WM_TIMER:
//
// Timer set to dispatch a WM_TIMER message has gone off.
// Display a little message and beep the speaker.
//
MessageBeep(0);
hdc = GetDC(hwnd);
wsprintf((LPSTR)&szText, "WM_TIMER sent to hwnd - %d ", counter++);
TextOut(hdc, 10, 10,(LPSTR)&szText, lstrlen((LPSTR)&szText));
//
// Now call Toolhelp to get the system timer numbers
//
ti.dwSize = sizeof(TIMERINFO);
if (!TimerCount((TIMERINFO FAR *)&ti))
{
ReleaseDC(hwnd, hdc);
break;
}
wsprintf((LPSTR)&szText, "System: %lu, this VM: %lu ", ti.dwmsSinceStart, ti.dwmsThisVM);
TextOut(hdc, 10, 40, (LPSTR)&szText, lstrlen((LPSTR)&szText));
ReleaseDC(hwnd, hdc);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return(DefWindowProc(hwnd,
}
return(0);
}
//------------------------
// StartTimer
//
// Starts a timer to either dispatch a WM_TIMER message or call a callback.
// Care is taken to save the proper hWnd and timer ID for use when killing
// the timer.
//------------------------
void StartTimer(HWND hwnd, int Type, int Speed)
{
switch (Type) {
//
// Create a timer to dispatch a WM_TIMER message.
//
case cmdToHwnd:
if (SetTimer(hwnd, 1, Millisec, NULL) == NULL)
{
MessageBox(hwnd, "Couldn't create timer.", "Notice!", MB_OK);
return;
}
hwndKill = hwnd;
idKill = 1;
break;
//
// Create a timer to call a callback function. If we are
// changing only the timer rate, be sure that the ID parameter is
// the idKill value returned from the initial SetTimer call.
//
case cmdCallProc:
if ((idKill = SetTimer(NULL, (idKill ? idKill : 1), Millisec, fpTimerCallback)) == NULL)
{
MessageBox(hwnd, "Couldn't create timer.", "Notice!", MB_OK);
return;
}
hwndKill = NULL;
break;
}
}
//------------------------
// TimerCallbackProc
//
// Callback function that is called when a timer setup to call the callback
// goes off. Displays a short message and beeps the speaker.
//------------------------
WORD FAR PASCAL __export TimerCallbackProc(HWND hwnd, WORD msg, int idEvent, DWORD dwtime)
{
HDC hdc;
MessageBeep(0);
hdc = GetDC(hwndMain);
wsprintf((LPSTR)&szText, "Callback function called - %d ", counter++);
TextOut(hdc, 10, 10, (LPSTR)&szText, lstrlen((LPSTR)&szText));
//
// Now call Toolhelp to get the system timer numbers
//
ti.dwSize = sizeof(TIMERINFO);
if (!TimerCount((TIMERINFO FAR *)&ti))
{
ReleaseDC(hwnd, hdc);
return(0);
}
wsprintf((LPSTR)&szText, "System: %lu, this VM: %lu ", ti.dwmsSinceStart, ti.dwmsThisVM);
TextOut(hdc, 10, 40, (LPSTR)&szText, lstrlen((LPSTR)&szText));
ReleaseDC(hwndMain, hdc);
return(0);
}
/* Filename: Timers.def */
;-------------------------
; Windows Timer Sample Application
;-------------------------
NAME TIMERS
DESCRIPTION 'Sample Timer Application'
EXETYPE WINDOWS
STUB 'WINSTUB.EXE'
CODE MOVEABLE PRELOAD
DATA MOVEABLE MULTIPLE PRELOAD
HEAPSIZE 512
EXPORTS
TimerTestWndProc
TimerCallbackProc
/* Filename: Timers.h */
//{{NO_DEPENDENCIES}}
// App Studio generated include file.
// Used by TIMERS.RC
//
#define _APS_NEXT_COMMAND_VALUE 101
#define _APS_NEXT_SYMED_VALUE 101
#define _APS_NEXT_RESOURCE_VALUE 102
#define cmdStop 0x200
#define cmdToHwnd 0x201
#define cmdCallProc 0x202
#define cmdSlow 0x203
#define cmdMedium 0x204
#define cmdFast 0x205
#define _APS_NEXT_CONTROL_VALUE 1000
/* Filename: Timers.mak */
# Microsoft Visual C++ generated build script - Do not modify
PROJ = TIMERS
DEBUG = 1
PROGTYPE = 0
CALLER =
ARGS =
DLLS =
D_RCDEFINES =
R_RCDEFINES =
ORIGIN = MSVC
ORIGIN_VER =1.00
PROJPATH =UNKNOWN
USEMFC = 0
CC = cl
CPP = cl
CXX = cl
CCREATEPCHFLAG =
CPPCREATEPCHFLAG =
CUSEPCHFLAG =
CPPUSEPCHFLAG =
FIRSTC = TIMERS.C
FIRSTCPP =
RC = rc
CFLAGS_D_WEXE =/nologo /YX /G2 /W3 /AS /Od /D "_DEBUG" /FR /GA /Zi
CFLAGS_R_WEXE =/nologo /YX /G2 /W3 /AS /Ox /D "NDEBUG" /FR /GA /Gs
LFLAGS_D_WEXE =/NOLOGO /NOD /STACK:4096 /ALIGN:16 /ONERROR:NOEXE /CO
LFLAGS_R_WEXE =/NOLOGO /NOD /STACK:4096 /ALIGN:16 /ONERROR:NOEXE
LIBS_D_WEXE = oldnames libw commdlg shell olecli olesvr slibcew libw toolhelp.lib
LIBS_R_WEXE = oldnames libw commdlg shell olecli olesvr slibcew libw toolhelp.lib
RCFLAGS =/NOLOGO
RESFLAGS =/NOLOGO /t
RUNFLAGS =
DEFFILE = TIMERS.DEF
OBJS_EXT =
LIBS_EXT =
!if "$(DEBUG)" == "1"
CFLAGS = $(CFLAGS_D_WEXE)
LFLAGS = $(LFLAGS_D_WEXE)
LIBS = $(LIBS_D_WEXE)
MAPFILE = nul
RCDEFINES = $(D_RCDEFINES)
!else
CFLAGS = $(CFLAGS_R_WEXE)
LFLAGS = $(LFLAGS_R_WEXE)
LIBS = $(LIBS_R_WEXE)
MAPFILE = nul
RCDEFINES = $(R_RCDEFINES)
!endif
!if [if exist MSVC.BND del MSVC.BND]
!endif
SBRS = TIMERS.SBR
all: $(PROJ).EXE $(PROJ).BSC
TIMERS.OBJ: TIMERS.C $(TIMERS_DEP)
$(CC) $(CFLAGS) $(CCREATEPCHFLAG) /c TIMERS.C
TIMERS.RES: TIMERS.RC $(TIMERS_RCDEP)
$(RC) $(RCFLAGS) $(RCDEFINES) -r TIMERS.RC
$(PROJ).EXE:: TIMERS.RES
$(PROJ).EXE:: TIMERS.OBJ $(OBJS_EXT) $(DEFFILE)
echo >NUL @<<$(PROJ).CRF
TIMERS.OBJ +
$(OBJS_EXT)
$(PROJ).EXE
$(MAPFILE)
$(LIBS)
$(DEFFILE);
<<
link $(LFLAGS) @$(PROJ).CRF
$(RC) $(RESFLAGS) TIMERS.RES $@
@copy $(PROJ).CRF MSVC.BND
$(PROJ).EXE:: TIMERS.RES
if not exist MSVC.BND $(RC) $(RESFLAGS) TIMERS.RES $@
run: $(PROJ).EXE
$(PROJ) $(RUNFLAGS)
$(PROJ).BSC: $(SBRS)
bscmake @<<
/o$@ $(SBRS)
<<
/* Filename: Timers.rc */
//Microsoft App Studio generated resource script.
//
#include "timers.h"
//////////////////////////
//
// Menu
//
TIMERMENU MENU DISCARDABLE
BEGIN
POPUP "&Timers"
BEGIN
MENUITEM "Timer stopped", cmdStop, CHECKED
MENUITEM "WM_TIMER to hwnd", cmdToHwnd
MENUITEM "Call timer proc", cmdCallProc
MENUITEM SEPARATOR
MENUITEM "Slow (5 second)", cmdSlow, CHECKED
MENUITEM "Medium (1 second)", cmdMedium
MENUITEM "Fast (.25 second)", cmdFast
END
END
#ifdef APSTUDIO_INVOKED
//////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE DISCARDABLE
BEGIN
"timers.h\0"
END
2 TEXTINCLUDE DISCARDABLE
BEGIN
"\0"
END
3 TEXTINCLUDE DISCARDABLE
BEGIN
"\r\n"
"\0"
END
//////////////////////////
#endif // APSTUDIO_INVOKED
#ifndef APSTUDIO_INVOKED
//////////////////////////
//
// From TEXTINCLUDE 3
//
//////////////////////////
#endif // not APSTUDIO_INVOKED