INFO: SetTimer() Should Not Be Used in Console Applications

ID: Q102482

The information in this article applies to:

SUMMARY

SetTimer() was not designed to be used with a console application because it requires a message loop to dispatch the timer signal to the timer procedure. In a console application, this behavior can be easily emulated with a thread that is set to wait on an event.

MORE INFORMATION

In Windows NT 3.1, SetTimer() can work within a console application, but it requires a thread in a loop calling GetMessage() and DispatchMessage().

For example:

   while (1)
   {
      GetMessage();
      DispatchMessage();
   }

Because this requires a thread looping, there is no real advantage to adding a timer to a console application over using a thread waiting on an event.

Another option is to use a multimedia timer, which does not require a message loop and has a higher resolution. In Windows NT 3.5, the resolution can be set to 1 msec using timeBeginPeriod(). See the help for timeSetEvent() and the Multimedia overview. Any application using Multimedia calls must include MMSYSTEM.H, and must link with WINMM.LIB.

Additional query words:

Keywords          : kbNTOS kbGrpUser kbWinOS95 kbWinOS98 kbWndw kbWndwMsg 
Issue type        : kbinfo

Last Reviewed: December 24, 1998