WaitForExit();

A

Alistair George

How permanent is WaitForExit(); ? I mean is there anything/ an
alternative one can do to break the wait otherwise I will run my thread
in a separate function, and control it by global variables.
Reason - a CMD.EXE thread, with a console application running, which we
need to cancel out of prior to the thread running its course.
Here is what we have:
ProcessStartInfo psi = new
ProcessStartInfo("CMD.EXE",query);
psi.UseShellExecute = useShellExec;
psi.RedirectStandardOutput = stdout;
psi.CreateNoWindow = noWindow;
hbProc = new Process();
hbProc = Process.Start(psi);
hbProc.WaitForExit();


Thanks,
Alistair.
 
M

Marc Gravell

Well, you could use:

while(!hbProc.WaitForExit(someTimeout)) {
if(ShouldCancel) break;
}

Marc
 
D

Doug Semler

Well, you could use:

while(!hbProc.WaitForExit(someTimeout)) {
if(ShouldCancel) break;

}

Marc

(note, there is no reason to call both the new Process() and the
Process.Start(psi);

Or you could use:

hbProc = new Process();
hbProc.EnableRaisingEvents = true;
hbProc.Exited += new EventHandler(functionToCallAfterProcessExit);
hbProc.StartInfo = psi;
hbProc.Start();


// other thread - on want to cancel event:
hbProc -= new EventHandler(functionToCallAfterProcessExit);

In either case, just be aware of the potential race condition between
the process exiting and the desire to cancel the wait...
 
A

Arnshea

How permanent is WaitForExit(); ? I mean is there anything/ an
alternative one can do to break the wait otherwise I will run my thread
in a separate function, and control it by global variables.
Reason - a CMD.EXE thread, with a console application running, which we
need to cancel out of prior to the thread running its course.
Here is what we have:
ProcessStartInfo psi = new
ProcessStartInfo("CMD.EXE",query);
psi.UseShellExecute = useShellExec;
psi.RedirectStandardOutput = stdout;
psi.CreateNoWindow = noWindow;
hbProc = new Process();
hbProc = Process.Start(psi);
hbProc.WaitForExit();

Thanks,
Alistair.

You can either kill the thread running WaitForExit() or use the
overload that allows you to specify a timeout bool WaitForExit(int
msToWait). If you run it in a separate thread and kill it you're
going to get an interrupted exception. You may want to make sure the
process you started isn't still running (if it is, kill it).
 
A

Alistair George

You can either kill the thread running WaitForExit() or use the
overload that allows you to specify a timeout bool WaitForExit(int
msToWait). If you run it in a separate thread and kill it you're
going to get an interrupted exception. You may want to make sure the
process you started isn't still running (if it is, kill it).

Thanks guys with the suggestions which are useful. Will advise what
option worked best.
Al.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top