🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #1470

Re: SetConsoleCtrlHandler not processing close messages

#1470
From: Bob Gonder
Date: Thu, 09 Aug 2007 14:41
67 lines
2945 bytes
Mike Collins wrote:

>Hi all, posted a few days ago about a console application i was writing, 
>specifically processing messages within.

I don't use a message loop in my console app, but this is how I handle the handler.


static void EmergencyShutdown(void)	/*  we are getting out of here the hard way  */
{	throw_away_keys();		/*  toss any queued keys  */	
#if	UDP
	if( UDPRunning )
		KillUDP();	/* up to 5 sec delay */
#endif
	AppCanceled = TRUE;

	UnwindLockQue();
	UnwindFileQue();
	UnwindManagedIndexes();

	/*  remove myself from the handler queue  */
	SetConsoleCtrlHandler( &CtrlHandler, FALSE);
}

static BOOL _stdcall CtrlHandler(DWORD fdwCtrlType)
{
    switch (fdwCtrlType)
    { 
	/* Handle the CTRL+C signal. */ 
	case CTRL_C_EVENT: 
		push_key(CtrlC);	/*  pass Ctrl-C to app  */
		return TRUE;
 
	/* CTRL+CLOSE: confirm that the user wants to exit. */
/*	Ctrl_Close _needs_ to return False because I don't want a dialog poping up.
	Other events return True because I handled them this time,
	 and disabled myself so subsequent events will be false.
*/
	case CTRL_CLOSE_EVENT:
	case CTRL_BREAK_EVENT:
		EmergencyShutdown();
		return FALSE;	// next handler terminates me
 
	/* Pass other signals to the next handler. */ 
	/* Next handler will pop a terminate dialog */
	case CTRL_LOGOFF_EVENT:
	case CTRL_SHUTDOWN_EVENT: 
		EmergencyShutdown();
		return TRUE;
	default:
		return FALSE;
    }; 
}

Then, all sub-threads, such as a watchdog timer, check AppCanceled.
The application itself isn't going to run after I return.

>  The code seems to work but when i hit 
>Ctrl+Break the console application does not terminate.

Thanks, mine wasn't working either, then I switched it to FALSE
(see above), it terminated fine.

It could well be that because you don't discriminate in the handler,
that you enter an endless PostQuit-Close loop.
I would suggest turning off the handler within the handler.
That would eliminate the looping.
Technical Details
Message-ID: <7l6mb31h8sge2ls6ecj4df89vql95kgqc3@4ax.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <46bae4a2$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,d5cddcb589c8cc6f
X-Google-Attributes: gid10cc67,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: Bob Gonder <notbg@notmindspring.invalid>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: SetConsoleCtrlHandler not processing close messages
Date: Thu, 09 Aug 2007 07:41:16 -0700
Organization: MindSpring Enterprises, Inc.
Reply-To: notbg@notmindspring.invalid
Message-ID: <7l6mb31h8sge2ls6ecj4df89vql95kgqc3@4ax.com>
References: <46bae4a2$1@newsgroups.borland.com>
X-Newsreader: Forte Agent 1.93/32.576 English (American)
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 69.69.86.191
X-Trace: newsgroups.borland.com 1186670473 69.69.86.191 (9 Aug 2007 07:41:13 -0700)
Lines: 70
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail