You are here:

C++/Timers in C++, please help


I am creating a program in VC++, a macro, just point and click for now.

I am having trouble setting an interval between two clicks, so it does not execute all clicks at once.

The current code I am using is:

void sleep(int seconds) {
int endwait;
 endwait = clock() + seconds * CLOCKS_PER_SEC;
 while (clock() < endwait) { //nothing }

This, as you can see, causes an enormous amount of CPU activity, and usually causes 80% of my CPU activity.

Would you happen to know any other code that would fix/minimalise this?

Thank you for your time,

Steven , Thank you for your question.

Here is some source code to show one way to solve your problem. IMPORTANT NOTE: This Windows code may not compile because it is dependent on other parts of my personal application library. It should be sufficient to give you some ideas; I cannot supply you with all of the source code needed to make this example actually work. Note the failure of to provide for indenting source code.

//   ----------------------------------------------------------------------------
//   Name:         function Wait
//   Description:   Sleep for n msec, yielding the CPU to other processes;
//          n can be 0.
//   ----------------------------------------------------------------------------
EXPORT VOID         Wait(long Msec)
  DWORD         End;
  MSG          Msg;

  if (Msec <= 0)
  End = GetTickCount() + Msec;
  for (;;)
     if (AppIsClosing)
     if (GetTickCount() >= End)
     } // Loop
  } // Wait

//   ----------------------------------------------------------------------------
//   Name:         function YieldCPU
//   Description:   Handles zero or one Windows message(s), to yield this
//          process to others; return FALSE if no messages are queued
//          for this thread.
//   ----------------------------------------------------------------------------
EXPORT BOOL         YieldCPU(MSG * pMsg/* = NULL*/,
  MSG          Msg;
  BOOL         Status;

  // Avoid deadlock on modal call like MessageBox or PeekMessage
  if (InSendMessage())
     return FALSE;

  if (!pMsg)
     pMsg = &Msg;
  Status = PeekMessage(pMsg, NULL, 0, 0, PM_REMOVE);
  if (!Status)
     // No message available
     return FALSE;
  if (pMsg->message == WM_QUIT)
     // Defer the WM_QUIT msg to the modal wait loop, if any
     return FALSE;
     } // WM_QUIT was received
  if (pMsg->hwnd)
     HandleMessage(pMsg, NULL, Flags);
  return TRUE;
  } // YieldCPU

//   ----------------------------------------------------------------------------
//   Name:         function HandleMessage
//   Description:   Handle one dialog box or other Windows message. Used in
//          modal dlg wait loop.
//   ----------------------------------------------------------------------------
EXPORT VOID         HandleMessage(MSG * pMsg, HWND DlgWnd/*= NULL*/,
  if (!IsWindow(pMsg->hwnd))
     pMsg->hwnd = MainWnd;

  // Now done in CDlgBase::ModalWait:
  // Detect special msgs sent directly to the app thread
  if (!DlgWnd)
     DlgWnd = GetActiveWindow();

  // (DlgWnd assumed to be of this process)

  // Handle Accelerator keys before IsDialogMessage handles them
  HACCEL      Accelerators;

     Accelerators = LoadAccelerators(AppInstance, "MAIN");
     if (Accelerators &&
        TranslateAccelerator(DlgWnd, Accelerators, pMsg))

  // Handle dialog box messages
     if (IsDialogMessage(DlgWnd, pMsg))

  // Handle other msgs
  } // HandleMessage

David Spector


All Answers

Answers by Expert:

Ask Experts


David Spector


Highly knowledgeable in the C++ language, Visual C++ (MSVC), Windows API, documentation and other quality-assurance techniques, and debugging. Knowledgeable in MFC, COM, GUI design, and object-oriented design.


I have been a software engineer since 1965. I have been published. My specializations have been: biomedical programming, compiler implementation, and many kinds of Windows programming. I don't do Databases or other business-oriented stuff.

Windows?/DOS Developer's Journal, ACM SIGPLAN Notices, and Computer Science Press.

ICCP Systems Programming Certification
Master's degree equivalent in Computer Science

©2016 All rights reserved.