Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Keep program open and running

Keep program open and running

Scheduled Pinned Locked Moved ATL / WTL / STL
questionc++tools
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lucidation
    wrote on last edited by
    #1

    First off, I'm still new to coding in C++ and so I hope this is the right forum for my question... I'm coding a very small utility for a niche need we've got where I work. Right now I've got things coded so that it uses CreateProcess to fire off another program we run. What I need to do is then have this utility stay open and running (the program that it kicks off can terminate the original program once it's done doing its thing - and I can't just wait for a return code because the program that gets kicked off may kick off others, and one of those might be the final item to run). Right now it launches our program and then, since it's done doing all the code, it finishes up and quits. Is there any way to tell the thing to stay open and running? Kind of like a service (but not a service because I don't want to install anything).

    L S 2 Replies Last reply
    0
    • L Lucidation

      First off, I'm still new to coding in C++ and so I hope this is the right forum for my question... I'm coding a very small utility for a niche need we've got where I work. Right now I've got things coded so that it uses CreateProcess to fire off another program we run. What I need to do is then have this utility stay open and running (the program that it kicks off can terminate the original program once it's done doing its thing - and I can't just wait for a return code because the program that gets kicked off may kick off others, and one of those might be the final item to run). Right now it launches our program and then, since it's done doing all the code, it finishes up and quits. Is there any way to tell the thing to stay open and running? Kind of like a service (but not a service because I don't want to install anything).

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Just add a loop with a timer, and whatever other functions you need. The sequence would then be something like:

      while TRUE
      if applicationFlag is TRUE
      then
      start external application
      set applicationFlag FALSE
      end if
      sleep for some seconds
      end while

      You will need to add some other decision code to set the applicationFlag to TRUE at some point.

      One of these days I'm going to think of a really clever signature.

      L 1 Reply Last reply
      0
      • L Lost User

        Just add a loop with a timer, and whatever other functions you need. The sequence would then be something like:

        while TRUE
        if applicationFlag is TRUE
        then
        start external application
        set applicationFlag FALSE
        end if
        sleep for some seconds
        end while

        You will need to add some other decision code to set the applicationFlag to TRUE at some point.

        One of these days I'm going to think of a really clever signature.

        L Offline
        L Offline
        Lucidation
        wrote on last edited by
        #3

        What if the program remains open for an hour or three (I hope they close it sooner, but I have to think of the "what ifs")? Is the Sleep command going to be the right way to go, or am I going to create a problem when using it over an extended period of time?

        L 1 Reply Last reply
        0
        • L Lucidation

          What if the program remains open for an hour or three (I hope they close it sooner, but I have to think of the "what ifs")? Is the Sleep command going to be the right way to go, or am I going to create a problem when using it over an extended period of time?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          The other alternative is to use a timer: Timers Tutorial[^] is a great article which shows how to implement a few.

          One of these days I'm going to think of a really clever signature.

          1 Reply Last reply
          0
          • L Lucidation

            First off, I'm still new to coding in C++ and so I hope this is the right forum for my question... I'm coding a very small utility for a niche need we've got where I work. Right now I've got things coded so that it uses CreateProcess to fire off another program we run. What I need to do is then have this utility stay open and running (the program that it kicks off can terminate the original program once it's done doing its thing - and I can't just wait for a return code because the program that gets kicked off may kick off others, and one of those might be the final item to run). Right now it launches our program and then, since it's done doing all the code, it finishes up and quits. Is there any way to tell the thing to stay open and running? Kind of like a service (but not a service because I don't want to install anything).

            S Offline
            S Offline
            Stephen Hewitt
            wrote on last edited by
            #5

            If I'm understanding correctly (I'm not certain I am, your description is anything but clear) something like this might help (you can wait on a process handle):

            STARTUPINFO si = { sizeof(si) };
            PROCESS_INFORMATION pi;
            BOOL bOk = CreateProcess(
            _T("C:\\Windows\\system32\\notepad.exe"),
            NULL,
            NULL,
            NULL,
            FALSE,
            0,
            NULL,
            NULL,
            &si,
            &pi
            );
            if (bOk)
            {
            CloseHandle(pi.hThread); // We don't need so don't leak it!
            WaitForSingleObject(pi.hProcess, INFINITE);
            CloseHandle(pi.hProcess); // We're finished with it.

            MessageBox(NULL, \_T("Finished"), \_T("Create and wait"), MB\_OK);
            

            }

            Steve

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups