Closing console leads to app termination

Just starting out? Need help? Post your questions and find answers here.
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Closing console leads to app termination

Post by Lunasole »

In following example, even if it is not compiled to console mode, if user closes console using "close" button, this will terminate application process, while closing console by CloseConsole() is fine and continues loop.
I'm not sure is it PB or Windows problem, tested with PB 5.5 and WinXP/7.

I was using console window to display some info from DLL and that was not funny when closing that window with button crashed whole host-processs ^^

Code: Select all

EnableExplicit
Define isConsole, WasOpened
Define StartTime = ElapsedMilliseconds()

Repeat
	; here just opening console once if it was not opened already
	If Not isConsole And Not WasOpened
		isConsole = OpenConsole("test")
		PrintN("Press any key to close console (NOT TO QUIT APP)...")
		WasOpened = 1
	EndIf
			
	; checking console input and close console window if any key pressed
	If isConsole
		Inkey()
		If RawKey()
			CloseConsole()
			isConsole = 0
			; now console window is closed and all is fine, loop continues to work
		EndIf
	EndIf

	; simulate application quit after 30 seconds
	If Abs(ElapsedMilliseconds() - StartTime) >= 30000
		Break
		;
	EndIf
	
	Delay(128)
ForEver

; BUG: process terminated and this is never called if user closed console using "close" button instead of pressing any key
MessageRequester("Quitting", "")

; clean up on quit
If isConsole
	CloseConsole()
	isConsole = 0
EndIf
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
freak
PureBasic Team
PureBasic Team
Posts: 5948
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Closing console leads to app termination

Post by freak »

This is the default behavior for a console application (or one that opens a console Window). Its not a bug (and not a crash either). The program is simply terminated by the default handler for these events.

You can change this behavior by defining your own handler for such events:
https://msdn.microsoft.com/de-de/librar ... 86016.aspx
quidquid Latine dictum sit altum videtur
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Closing console leads to app termination

Post by Lunasole »

@freak, thanks, I didn't knew about such handlers

UPS. It's however didn't helped to avoid whole process termination
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
Post Reply