467,136 Members | 1,328 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,136 developers. It's quick & easy.

WaitForExit();

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.
Sep 19 '07 #1
  • viewed: 8090
Share:
4 Replies
Well, you could use:

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

Marc
Sep 19 '07 #2
On Sep 19, 5:15 am, "Marc Gravell" <marc.grav...@gmail.comwrote:
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...

Sep 19 '07 #3
On Sep 19, 4:11 am, Alistair George <non...@xtra.co.nzwrote:
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).

Sep 19 '07 #4
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.
Sep 19 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Christian Billig | last post: by
4 posts views Thread by Terry Brown | last post: by
1 post views Thread by Ebbe Kristensen | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.