title: C++ monitor running processes in the background?
body: <p>I've been trying to write a program in C++ that will monitor running processes in the background and terminate a certain one if it's detected to be running. I have written a program that will do so, however the only way I can think of to do this is to use an infinite <code>WHILE</code> loop that keeps checking for the program. This, as you can imagine, constantly uses CPU power and resources to be constantly looping. In task manager, you can see that most processes that are running are always using 0% of the CPU. My question is: How can I write or modify this program to run in the background, utilizing 0% of the CPU until it detects the process it's supposed to terminate?
  My entire program is below. In this example, I have used "Notepad.exe" in <code>WinMain</code> as the process the program should be terminating.</p>
  
  <pre><code>#include &lt;Windows.h&gt;
  #include &lt;iostream&gt;
  #include &lt;tlhelp32.h&gt;
  #include &lt;string&gt;
  #define TA_FAILED 0
  #define TA_SUCCESS_CLEAN 1
  #define TA_SUCCESS_KILL 2
  
  DWORD WINAPI TerminateApp(DWORD dwPID, DWORD dwTimeout);
  DWORD WINAPI Terminate16App(DWORD dwPID, DWORD dwThread, WORD w16Task, DWORD dwTimeout);
  
  typedef struct {
     DWORD dwID;
     DWORD dwThread;
  } TERMINFO;
  
  BOOL CALLBACK TerminateAppEnum( HWND hwnd, LPARAM lParam ) ;
  
  DWORD WINAPI TerminateApp( DWORD dwPID, DWORD dwTimeout ) {
    HANDLE   hProc ;
    DWORD   dwRet ;
  
    // If we can't open the process with PROCESS_TERMINATE rights,
    // then we give up immediately.
    hProc = OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE,
       dwPID);
  
    if(hProc == NULL) {
       return TA_FAILED ;
    }
  
    // TerminateAppEnum() posts WM_CLOSE to all windows whose PID
    // matches your process's.
    EnumWindows((WNDENUMPROC)TerminateAppEnum, (LPARAM) dwPID) ;
  
    // Wait on the handle. If it signals, great. If it times out,
    // then you kill it.
    if(WaitForSingleObject(hProc, dwTimeout)!=WAIT_OBJECT_0)
       dwRet=(TerminateProcess(hProc,0)?TA_SUCCESS_KILL:TA_FAILED);
    else
       dwRet = TA_SUCCESS_CLEAN ;
  
    CloseHandle(hProc) ;
  
    return dwRet ;
  }
  
  BOOL CALLBACK TerminateAppEnum( HWND hwnd, LPARAM lParam ) {
    DWORD dwID ;
  
    GetWindowThreadProcessId(hwnd, &amp;dwID) ;
  
    if(dwID == (DWORD)lParam) {
       PostMessage(hwnd, WM_CLOSE, 0, 0) ;
    }
    return TRUE ;
  }
  
  DWORD FindProcessId(const std::string&amp; processName);
  
  int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
  
      std::string process1 = "Notepad.exe";
      while (1) {
      TerminateApp(FindProcessId(process1),0);
      }
  return 0;
  }
  
  DWORD FindProcessId(const std::string&amp; processName) {
  
     PROCESSENTRY32 processInfo;
     processInfo.dwSize = sizeof(processInfo);
  
     HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
     if (processesSnapshot == INVALID_HANDLE_VALUE) {
        return 0;
     }
     Process32First(processesSnapshot, &amp;processInfo);
     if (!processName.compare(processInfo.szExeFile)) {
        CloseHandle(processesSnapshot);
        return processInfo.th32ProcessID;
     }
  
     while (Process32Next(processesSnapshot, &amp;processInfo)) {
  
        if (!processName.compare(processInfo.szExeFile)) {
           CloseHandle(processesSnapshot);
           return processInfo.th32ProcessID;
        }
     }
     CloseHandle(processesSnapshot);
     return 0;
  }
  </code></pre>