Page 1 of 1

Closing console leads to app termination

Posted: Tue Sep 13, 2016 4:21 pm
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

Re: Closing console leads to app termination

Posted: Tue Sep 13, 2016 11:08 pm
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

Re: Closing console leads to app termination

Posted: Tue Sep 13, 2016 11:44 pm
by Lunasole
@freak, thanks, I didn't knew about such handlers

UPS. It's however didn't helped to avoid whole process termination