You are here:

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

Advertisement


Question
Hello,
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

Answer
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 AllExperts.com 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)
     return;
  End = GetTickCount() + Msec;
  for (;;)
     {
     if (AppIsClosing)
        return;
     YieldCPU(&Msg);
     if (GetTickCount() >= End)
        break;
     } // 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_HANDLING_FLAGS Flags/* = MSG_HANDLING_DEFAULT*/)
  {
  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
     PostQuitMessage(0);
     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*/,
         MSG_HANDLING_FLAGS Flags/* = MSG_HANDLING_DEFAULT*/)
  {
  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;

  if (!IS_FLAG_SET_IN_ARG(MSG_HANDLING_SUPPRESS_ACCEL) &&
     !SuppressAccelerators)
     {
     Accelerators = LoadAccelerators(AppInstance, "MAIN");
     if (Accelerators &&
        TranslateAccelerator(DlgWnd, Accelerators, pMsg))
        return;
     }

  // Handle dialog box messages
  if (!IS_FLAG_SET_IN_ARG(MSG_HANDLING_SUPPRESS_IsDialogMessage))
     {
     if (IsDialogMessage(DlgWnd, pMsg))
        return;
     }

  // Handle other msgs
  TranslateMessage(pMsg);
  DispatchMessage(pMsg);
  } // HandleMessage

David Spector

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


David Spector

Expertise

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.

Experience

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.

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

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

©2016 About.com. All rights reserved.