Something similar to #PB_Event_CloseWindow for a console?

Just starting out? Need help? Post your questions and find answers here.
Quin
Addict
Addict
Posts: 1136
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Something similar to #PB_Event_CloseWindow for a console?

Post by Quin »

The server for my networking application runs as a console. It all works fine, even working as a systemd service on my actual server. When running it from source on Windows to test something though, I noticed that it would apparently crash if I hit alt+f4. The status bar wouldn't show any errors, nothing would show up as an error line, but it would say the debug executable quit unexpectedly. The main loop of the application is a Repeat : ForEver loop that calls NetworkServerEvent() every iteration, and nothing else. I'm wondering if I need to capture some sort of console closed event and shut down more cleanly. I'm not sure when this started so it's entirely possible it's something super weird in the code, but I want to rule this out first. Is there a method for capturing when the console closes, whether that be with control+c, alt+f4, etc?
Thanks.
User avatar
mk-soft
Always Here
Always Here
Posts: 6321
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Something similar to #PB_Event_CloseWindow for a console?

Post by mk-soft »

The control command Ctrl-C can be intercepted and ignored. Alt-F4 (Close) can be queried but not cancelled.
MSDN: https://learn.microsoft.com/en-us/windo ... r-function

Code: Select all

Procedure myConsoleHandler(dwControlType.l)
  Protected r1
  Select dwControlType
    Case #CTRL_C_EVENT                      ; CTRL+C signal
      Debug "CTRL+C pressed"
      r1 = #True
    Case #CTRL_BREAK_EVENT                  ; CTRL+BREAK signal
      Debug "CTRL+BREAK pressed"
      r1 = #True
    Case #CTRL_CLOSE_EVENT                  ; Close button pressed or End Task in TaskManager
      Debug "Close console signal"
      r1 = #False
    Case #CTRL_LOGOFF_EVENT                 ; user is logging off
      Debug "User is logging off"
      r1 = #False
    Case #CTRL_SHUTDOWN_EVENT               ; system is shutting down
      Debug "System shutdown signal"
      r1 = #False
  EndSelect
  ProcedureReturn r1
EndProcedure

DisableDebugger

If OpenConsole()
  
  SetConsoleCtrlHandler_(@myConsoleHandler(), #True) ; register handler
  
  Repeat
    PrintN("Exit with 'q'")
    
    a$ = Input()
  Until a$ = "q"
  
  SetConsoleCtrlHandler_(@myConsoleHandler(), #False) ; un-register handler
  
  CloseConsole()
EndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
jacdelad
Addict
Addict
Posts: 2032
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Something similar to #PB_Event_CloseWindow for a console?

Post by jacdelad »

Oh come on, will this be asked weekly now?
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Quin
Addict
Addict
Posts: 1136
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Something similar to #PB_Event_CloseWindow for a console?

Post by Quin »

jacdelad wrote: Sun Jan 28, 2024 7:48 pm Oh come on, will this be asked weekly now?
Perhaps if something is asked a lot, it would be good to document it somewhere, not show up in random topics with the snark turned up to 100, no?
User avatar
jacdelad
Addict
Addict
Posts: 2032
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Something similar to #PB_Event_CloseWindow for a console?

Post by jacdelad »

https://www.purebasic.fr/english/viewtopic.php?t=66591
https://www.purebasic.fr/english/viewtopic.php?p=475353
https://www.purebasic.fr/english/viewtopic.php?t=21334
https://www.purebasic.fr/english/viewtopic.php?t=82733
https://www.purebasic.fr/english/viewtopic.php?t=83399
...and so on. While there could be another sentence in the help about that, you can find numerous information asking Google with the keywords "purebasic", "console" and "close"/"prevent closing"/"alt f4","ctrl c"...
...and yes, I'm rude. Sorry.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Quin
Addict
Addict
Posts: 1136
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Something similar to #PB_Event_CloseWindow for a console?

Post by Quin »

jacdelad wrote: Mon Jan 29, 2024 8:46 pm https://www.purebasic.fr/english/viewtopic.php?t=66591
https://www.purebasic.fr/english/viewtopic.php?p=475353
https://www.purebasic.fr/english/viewtopic.php?t=21334
https://www.purebasic.fr/english/viewtopic.php?t=82733
https://www.purebasic.fr/english/viewtopic.php?t=83399
...and so on. While there could be another sentence in the help about that, you can find numerous information asking Google with the keywords "purebasic", "console" and "close"/"prevent closing"/"alt f4","ctrl c"...
...and yes, I'm rude. Sorry.
I also should've definitely worded my question better, because it extended slightly beyond just wanting a way to determine when someone tried to close it. My question was more intended to ask if there was a way to gracefully shut down a console app, because I'm experiencing some weird behavior where control+c or alt+f4 displays the debug executable quit unexpectedly, but no line is focused with an error message or anything. My application is a server that runs on a Repeat...ForEver loop, and my thought is that some connection isn't closed or it tries to do something with the console window, which was now closed, but this is the first time I've ever seen anything like it, so I frankly have no idea.
User avatar
jacdelad
Addict
Addict
Posts: 2032
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Something similar to #PB_Event_CloseWindow for a console?

Post by jacdelad »

There's an API to bend the app shutdown via close button to an own function, but I don't remember it (Google should help) and finally after calling the function, the app will shutdown anyway.
The console is not designed to be a companion to your app, but to control it and control it hard by the user, if needed (forced close, whatever the program is doing right now). With this in mind, the console is not always suitable.
There's a simulated, customizable console somewhere here in the forum, maybe this one helps you.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Post Reply