Thread: How to tell when internal program is done running

  1. #1
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728

    How to tell when internal program is done running

    Run into a bit of a problem here. I am creating a program that allows the user to add perl scripts to a queue and when the user clicks "Go" then the program calls and runs each script in order.

    The problem I am having is how does my code know when a perl script is finished so that it can start the next one WITHOUT having my program stop completely while it waits? I don't want the program to wait because many of the perl scripts take several hours and I want the user to be able to continually add to the queue even if a script is running. I'm not picky about which executable running method I use (ie ShellExecute, CreateProcess, etc), I'm OK with whatever works best to accomplish this. Basically what I am looking for is a message sent to my window proc that says "Hey, the last program just ended, so its time to pop the queue and run the next program". Something like this:
    Code:
    .....
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	switch (message)
    	{
                         .....
    
                          case BUTTON_GO:
                                  runTopScript();
                                  break;
                          case LATEST_PROGRAM_JUST_FINISHED:
                                  runTopScript();
                                  break;
    
                          ......
                     }
    }
    
    void runTopScript()
    {
                 // if queue is not empty, pop top of queue and run that perl script
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I would have a separate thread dedicated to running your scripts and determining when a script stops. Then I would use CreateProcess API to run the script (i imagine, not too familiar with Perl at all) and then call GetExitCodeProcess to determine when the process ended. You can look them up on MSDN for the parameters but basically you'll get STILL_ACTIVE from GetExitCodeProcess if the process is still running.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Handles to processes are synchronization objects that can be used by any of the wait functions.

    gg

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Quote Originally Posted by Codeplug
    Handles to processes are synchronization objects that can be used by any of the wait functions.

    gg
    Completely escaped me, yeah you're going to want to use WaitForSingleObject[Ex] to wait for the process to end.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Ok, the documentation is a little cryptic for someone like me who has no experience with multiple threads/processes, especially since there are no examples listed. Any chance for a quick example of simple Win32 code calling another program and being signalled when it finishes or possibly a good link to one? Google has been less than helpful, mostly because I'm having trouble formulating what exactly the search criteria should be for this one...

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Nevermind, not needed. Good ol' Petzold to the rescue, I was able to figure it out from his Multithreaded chapter.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 04-07-2008, 09:14 AM
  2. Program running long
    By smarta_982002 in forum Windows Programming
    Replies: 3
    Last Post: 03-27-2008, 05:24 AM
  3. Finding Program Running Time
    By pf732k3 in forum C Programming
    Replies: 6
    Last Post: 03-18-2008, 01:56 PM
  4. Check if program is alreadi running
    By rEtard in forum Windows Programming
    Replies: 1
    Last Post: 07-02-2005, 12:35 AM
  5. FAQ : running program inside program (spawn)
    By nipun in forum C Programming
    Replies: 3
    Last Post: 06-13-2004, 02:30 PM