Timeouts and Timers


Timers are required to determine and act on delays in user input. The functionality and differences between X Windows timeouts and Win32 timers is discussed in the following sections.

X Windows Timeouts

An X Windows client program can use the XtAddAppTimeOut() and XtRemoveTimeOut() functions with a callback to perform processing based an interval specified in milliseconds . The following code shows an example of this:

 //+ // perform task every one second //- void myTimerProc(Widget    w,                    XEvent   *event,                   String   *pars,                    Cardinal *npars) { } //+ // start a 1 second timer //- void startTimer(XtIntervalId *timer) {   (*timer) = XtAppAddTimeOut(gContext, 1000, myTimerProc, NULL); } //+ // stop the timer //- void stopTimer(XtIntervalId * timer) {   if(*timer) {     XtRemoveTimeOut(*timer);     (*timer) = NULL;   } } 

Win32 Timers

Windows timers can be used in two scenarios. First, as in the X Windows approach, a callback function can be identified to execute at each timer interval. Second, a message (WM_TIMER) can be used to process timer intervals.

The following example shows the callback version using Win32 timers:

 //+ // perform task every 1 second //- void CALLBACK myTimerProc(HWND      w,                    UINT      timerMessage,                   UINT_PTR  timerID,                    DWORD     systemTime) { } //+ // start a 1 second timer //- void startTimer(UINT_PTR *timer, UINT_PTR timerID) {   (*timer) = SetTimer(hParentWindow, timerID, 1000, myTimerProc); } //+ // stop the timer //- void stopTimer(UINT_PTR *timer) {   if(*timer) { KillTimer(hParentWindow, (*timer));   } } 

The following example shows Win32 timers using WM_TIMER:

 #define timer1 1 #define timer2 2 LRESULT CALLBACK WndProc(HWND hWnd,                            UINT message,                            WPARAM wParam,                            LPARAM lParam) {   switch (message) {   case WM_CREATE :     //+     //  create two timers     //-     SetTimer(hWnd, timer1, 1000, (TIMERPROC) NULL);     SetTimer(hWnd, timer2, 5000, (TIMERPROC) NULL);     break;   //+   //  wParam specifies that the timer identifier   //  returns zero (0) if this message is handled   //-   case WM_TIMER     switch (wParam) {         case timer1 :       // do timer 1 stuff       break;     case timer2 :       // do timer 2 stuff       break;     default :       break;     }         return (FALSE);   case WM_DESTROY :     //+     //  stop the two timers     //-         KillTimer(hWnd, timer1);     KillTimer(hWnd, timer2);          PostQuitMessage(0);     break; 



UNIX Application Migration Guide
Unix Application Migration Guide (Patterns & Practices)
ISBN: 0735618380
EAN: 2147483647
Year: 2003
Pages: 134

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net