-1

I need to check if a process is launched and then verify its exit code.

 EXProcess.StartInfo.FileName = strExE;
 EXProcess.StartInfo.Arguments = tempFile;
 EXProcess.Start();
 do
{}while (!rdcProcess.WaitForExit(1000));

if (EXProcess.HasExited)
 { ...}

EXProcess.Kill();

Now I have to close the windows that were opened by this process programmatically. In the sense I have to terminate everything that was started by this process.

The process is launching an exe. I have to check if it was able to successfully launch it, so I need the exit code. Hence I'm waiting till the code exits. Now after I get the exit code, the window launched by the process stil exists. I have to clos that window/somehow kill the process. but EXProcess.Kill gives an exception - mentioned in the comment

How do I do it? Please do help

worried
  • 29
  • 1
  • 5
  • 4
    If a process terminates, all its windows are gone too, so your question doesn't make much sense right now. What are you really trying to close? – Daniel Hilgarth Sep 25 '13 at 11:15
  • I guess grandchild processes opened by the child processes. This looks like a job for job objects( http://msdn.microsoft.com/en-us/library/windows/desktop/ms684161%28v=vs.85%29.aspx ), but I'm not sure these can be directly used from .Net... – Medinoc Sep 25 '13 at 11:21
  • I have edited the code above. I did trying killing the process. but it is raising an exception saying that I can't kill the process as it has already exited. – worried Sep 25 '13 at 12:36

1 Answers1

1

I think that's what you're trying to acheive:

using (var process = new Process())
{
    process.StartInfo.FileName = "notepad.exe";
    process.StartInfo.Arguments = "";

    process.Start();
    process.WaitForExit(1000);

    if (!process.HasExited)
        process.Kill();
}

This opens notepad.exe, and kill it if it's running for more than 1 seconds. As you can see the notepad.exe window is closed.

Benoit Blanchon
  • 13,364
  • 4
  • 73
  • 81
  • I think what the OP wants may be achieved by holding a reference to the process she opened, then killing it when her own program has exited. You are on the right track there, so +1. – Geeky Guy Sep 25 '13 at 12:47
  • I have refined my question above.. Can u plz help me wit it now? – worried Sep 25 '13 at 12:50