troubleshooting Question

How to add an option to terminate an infinite loop?

Avatar of KTN-IT
KTN-ITFlag for United States of America asked on
19 Comments1 Solution583 ViewsLast Modified:
At my organization, I am turning old XP machines into remote desktop clients by changing the Windows shell (at HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Shell) from explorer.exe to mstsc.exe.

Below is some code that starts a process (notepad, in this case) and then waits for it to be terminated, and starts it again.  This is what I want for my remote desktop clients, so when I user logs off from remote desktop, mstsc starts again to wait for another user to log in.

At some point, though, the process must be terminated, for instance, when I want to shut down or restart the computer.  How would I build in some functionality that would allow an exit from the infinite loop?  For instance, a certain key combination.
#include <windows.h>

void main ()
    memset(&pi, 0, sizeof(pi));
    memset(&si, 0, sizeof(si));
    si.cb = sizeof(si);
    TCHAR sCommandLine[MAX_PATH] = TEXT("C:\\windows\\NOTEPAD.EXE");

    BOOL result = 0;

    result = CreateProcess(NULL, sCommandLine,NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi);
    MSG msg;
    while(1) // infinite loop
        while (WAIT_OBJECT_0 != MsgWaitForMultipleObjects(1,&pi.hProcess,TRUE,INFINITE,QS_ALLINPUT))
          while(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) DispatchMessage(&msg);
        result = CreateProcess(NULL, sCommandLine,NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi);
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 19 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 19 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros