Gracefully kill a console app?

J

Jon Davis

Is there such a thing as a Windows standard for killing a console app that
is monitored by a System.Diagnostics.Process object?

I'm hosting several C# console apps in a C# Windows service and would like
to standardize on a graceful termination method when the Windows service
stops.

Jon
 
G

Guest

Hi Jon,

Here are a couple ideas that may or may not work for you, depending on your
application, which are probably much more graceful than a Kill on the
process. You have two separate processes and will need to get them to
communicate somehow. You could add a Cancel property to your console
application and have it check that property periodically. Then use Remoting
to allow the service to set that Cancel property.

Another potential option is to use WCF. You can have the console
applications host their own Web Service and then make the Windows Service
call the console Web Service, which would set the Cancel property.

I don't know if this will work for you, but maybe it is something you can use.

Joe
 
J

Jon Davis

So there isn't a standard Win32 "Exit Requested" signal for console apps.

Thanks.

Jon
 
W

Willy Denoyette [MVP]

Jon Davis said:
So there isn't a standard Win32 "Exit Requested" signal for console apps.

Thanks.

Jon

The closest you can get is by sending a CTRL+C to the console application's stdin, of course
this requires you to redirect the stdin and register a CTRL+C handler in the console
application.

Willy.
 
E

Ebbe Kristensen

Jon said:
So there isn't a standard Win32 "Exit Requested" signal for console
apps.

In an earlier project (a couple of years ago and not .NET) I looked very
hard for exactly that. If there is a way, it is undocumented.

So I ended up killing them, no questions asked.

Ebbe
 
L

Lucian Wischik

Jon Davis said:
Is there such a thing as a Windows standard for killing a console app that
is monitored by a System.Diagnostics.Process object?
I'm hosting several C# console apps in a C# Windows service and would like
to standardize on a graceful termination method when the Windows service
stops.

In addition to the other ideas in this thread: some console apps
terminate themselves naturally when they encounter an EOF on their
STDIN. So you could provide STDIN yourself through a pipe, and close
it when it's time. (it's not a standardized way: it'll be different
for each app.)
 

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