[Mono-dev] Control-C handler

Michi Henning michi at zeroc.com
Wed Jan 2 14:41:43 EST 2008

Jonathan Pryor wrote:
> So why can't setting a volatile variable within a signal handler be
> supported?  And/or use a Constrained Execution Region to *ensure* that
> the signal handler is JITed before the signal is emitted (does mono even
> support CERs yet?)?
Something to support signal handling would definitely be useful. I'm 
currently in the situation
where I'm maintaining a code base that must run on both .NET and Mono. 
The code is
a middleware library (www.zeroc.com) that must allow allow applications 
to react to
signals so they can shut down themselves and the Ice run time in an 
orderly fashion.

Under Windows, the code uses P/Invoke to call SetConsoleCtrlHandler so 
it can
intercept Ctrl-C, Ctrl-Break, and shutdown, logoff, and window closure. 
With Mono,
I'm pretty much stuck because there is no equivalent mechanism. As far 
as I can see, the only
option to achieve equivalent functionality is to create a C library that 
sets the necessary
signal handlers and to then P/Invoke into that library from my C# Mono 
code. But that
sort of defeats the purpose because I'd rather not ship a DLL for every 
possible platform
Mono runs on.

Console.CancelKeyPress, which was added with .NET 2.0, unfortunately 
doesn't work for
this purpose: it only intercepts keyboard interrupts, but doesn't 
trigger on logoff, shutdown, or
window closure.

One could argue that all this is a limitation of .NET, which should 
offer a way to reliably
intercept process termination. But, under Windows, I at least get an 
acceptable work-around
in the form of SetConsoleCtrlHandler whereas, with Mono, I don't.

So, anything to improve on this situation would be great. While I 
understand all the arguments
about what can and can't safely be done from within a signal handler, 
the need to react to
signals and perform some clean-up before a process goes away is a 
real-world requirement.
If Mono offered at least rudimentary support for signal handling that 
doesn't require P/Invoke
and external non-standard libraries, it would at least allow me to then 
build something on top
of that. Right now, I can't do anything much at all, as far as I can see.



More information about the Mono-devel-list mailing list