From the updated question and the comments, this seems to solve the problem:
Using the Windows timers is often not the best choice. For periodic actions that are hardware related and/or sensitive to system load, a worker thread may be a better solution:
- Create a worker thread and a terminate event.
- In the worker thread use
WaitForSingleObject() with the terminate event handle and your heart beat timeout inside a while loop.
- Upon the terminate event, leave the loop and return from the thread.
- Upon time out, execute your heart beat function.
- Upon program exit, fire the terminate event.
Optional set the thread priority one above the main app priority (see
). Your external process will probably have the same priority as your main app. Then the heart beat should work (if not delayed by heavy system load with higher priorities).