in reply to Re: Window Close / Process Termination Handler on Windows
in thread Window Close / Process Termination Handler on Windows

Hello TheloniusMonk,

Thanks for your feedback. Unfortunately adding BREAK to my list of signals still did not cause my code to trigger. The log is still empty after i close the window.

Updated Code which still does not trigger:
# partially based upon code by BrowserUK from https://www.perlmonks.or +g/?node_id=629210 use strict; use Config; use FindBin; use File::Spec; my $log_fn = File::Spec->catfile($FindBin::Bin, "cleanup_handler_log.t +xt"); my $LOG; open $LOG, '>>', $log_fn or die "Failed to open log '$log_fn' for appe +nding ($!)\n"; binmode $LOG; $LOG->autoflush(1); # disable buffering my @sigs = split ' ', $Config{ sig_name }; shift @sigs; print "SIGNALS: @sigs\n"; for ( @sigs ) { my $msg = "Signal $_ received\n"; $SIG{ $_ } = sub{ print $LOG $msg; warn $msg; }; } my $count; while( 1 ) { $count += 1 for 1.. 1e6; warn $count; sleep 3; }
The List of signals for which a signal handler is registered:
SIGNALS: HUP INT QUIT ILL NUM05 NUM06 NUM07 FPE KILL NUM10 SEGV NUM12 +PIPE ALRM TERM NUM16 NUM17 NUM18 NUM19 CHLD BREAK ABRT STOP NUM24 CONT CLD

Replies are listed 'Best First'.
Re^3: Window Close / Process Termination Handler on Windows
by TheloniusMonk (Sexton) on Aug 29, 2018 at 14:51 UTC
    In that case, the parent process is not signalling your process and it dies when the parent dies. So you need to write a subroutine in C that sets a handler,
    BOOL WINAPI SetConsoleCtrlHandler( _In_opt_ PHANDLER_ROUTINE HandlerRoutine, _In_ BOOL Add );
    and another that is called back. The handler routine can then issue a signal of its own for your perl program to trap. It's a bit of a maze but these are the set of functions you can choose from to manage the windows console: https://docs.microsoft.com/en-us/windows/console/console-functions

    Update: If you can live with uninterruptable, you could also consider a DOS wrapper that does "START /B wrapper2.bat", where wrapper2.bat runs the perl program. This should then be detached from the console window.

      I found some initial attempt to implement SetCtrlHandler in the source of Win32::Console. It is commented out in the C part and in the perl part. So i guess the author of the Module failed to get it running and chose to not implement it. Currently it seems to boil down to either attempting to implement the given Code for SetConsoleCtrlHandler myself, or to decouple my output logic from STDOUT, so i can background the process. Sniff. Would have preferred an easier solution.