• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 755
  • Last Modified:

Start processes, wait for it to end, and get its exit code (win32 api)

Hi

As the title indicates, I want to create a new processes using CreateProcesses, wait for it to terminated, and then recover its exit code.

Sample code would be helpful

-Mike
0
nerdmike
Asked:
nerdmike
1 Solution
 
MudieCommented:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdiagnosticsprocessclassexitcodetopic.asp
(the following is taken from the above link)
using System;
using System.Diagnostics;
using System.Threading;

namespace Process_Sample
{
   class MyProcessClass
   {
      public static void Main()
      {
         try
         {

            Process myProcess;
            myProcess = Process.Start("NotePad.exe");

            while(!myProcess.HasExited)
            {
               Console.WriteLine();

               // Get physical memory usage of the associated process.
               Console.WriteLine("Process's physical memory usage: " + myProcess.WorkingSet);
               // Get base priority of the associated process.
               Console.WriteLine("Base priority of the associated process: " + myProcess.BasePriority);
               // Get priority class of the associated process.
               Console.WriteLine("Priority class of the associated process: " + myProcess.PriorityClass);
               // Get user processor time for this process.
               Console.WriteLine("User Processor Time: " + myProcess.UserProcessorTime);
               // Get privileged processor time for this process.
               Console.WriteLine("Privileged Processor Time: " + myProcess.PrivilegedProcessorTime);
               // Get total processor time for this process.
               Console.WriteLine("Total Processor Time: " + myProcess.TotalProcessorTime);
               // Invoke overloaded ToString function.
               Console.WriteLine("Process's Name: " + myProcess.ToString());
               Console.WriteLine("-------------------------------------");

               if(myProcess.Responding)
               {
                  Console.WriteLine("Status:  Responding to user interface");
                  myProcess.Refresh();
               }
               else
               {
                  Console.WriteLine("Status:  Not Responding");
               }
               Thread.Sleep(1000);

            }

            Console.WriteLine();
            Console.WriteLine("Process exit code: {0}", myProcess.ExitCode);
         }
         catch(Exception e)
         {
            Console.WriteLine("The following exception was raised: " + e.Message);
         }
      }

   }
}

It looks like that does just what you want.
Hope it helps!
0
 
r-kCommented:
Here's an example (launches Notepad, then waits for it to finish):

    // Structures we need to create processes.
      STARTUPINFO si;
      PROCESS_INFORMATION piProcess;

      // Prepare a STARTUPINFO structre for
      // spawning processes.
      ZeroMemory(&si, sizeof(si));
      si.cb = sizeof(si);
      
      // Create the new process
      BOOL bRetVal = CreateProcess("c:\\windows\\notepad.exe",
            "c:\\windows\\notepad.exe",
            NULL, NULL, FALSE,0, NULL, NULL, &si, &piProcess);

      // Wait for other process to be done, before we proceed
      bRetVal=WaitForSingleObject(piProcess.hProcess,INFINITE);
      cout << "The other process has quit" << endl;
0
 
Julian HansenCommented:
Also, take a look at this article which demonstrates how to start a process but also how to retrieve the exit code using GetExitCodeProcess

http://support.microsoft.com/kb/q129796/
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now