[Mono-list] Closing console application on Linux
Jonathan Pryor
jonpryor at vt.edu
Thu May 1 14:12:07 EDT 2008
On Thu, 2008-05-01 at 12:57 -0400, Steve Harp wrote:
> I’m writing a console application to run on Linux. The application
> will have a database connection open and may also read/write text
> files at any time. How do I ensure that connections and files are
> closed properly if someone stops the application (Ctrl+Z, Ctrl+C,
> etc.)?
There are two answers:
1. You don't, because you can't -- the process is not alerted for many
Unix signals, including SIGSTOP (Ctrl+Z) and SIGKILL (kill -9).
Consequently, you might consider calling Stream.Flush() to (help) ensure
that the contents are (more likely to be) written to disk so that a
sudden process death isn't too problematic. This won't help for
database connections, though.
2. For a limited set of signals, you could use Mono.Unix.UnixSignal to
capture the signal, and poll the UnixSignal instance to see if it has
been set (i.e. the signal has been generated), and gracefully close your
app if the signal has been set. This works for SIGINT (Ctrl+C), etc.
http://www.go-mono.com/docs/monodoc.ashx?link=T:Mono.Unix.UnixSignal
- Jon
More information about the Mono-list
mailing list