Unwanted closing of console

Windows specific questions.
Post Reply
rvm
Posts: 7
Joined: Oct 08, 2017 3:34

Unwanted closing of console

Post by rvm »

I compiled a graphics program with –s gui. When I execute the .exe from a console window all works as it should – no additional console is created and the console from which it is being executed stays open when the program exits. However, when I execute it through a .bat file from a console window it closes that console when it exits. How can I prevent the console from closing when the program is executed through a .bat file?
hhr
Posts: 208
Joined: Nov 29, 2019 10:41

Re: Unwanted closing of console

Post by hhr »

Try PAUSE command in the bat-file.
rvm
Posts: 7
Joined: Oct 08, 2017 3:34

Re: Unwanted closing of console

Post by rvm »

rvm wrote:I compiled a graphics program with –s gui. When I execute the .exe from a console window all works as it should – no additional console is created and the console from which it is being executed stays open when the program exits. However, when I execute it through a .bat file from a console window it closes that console when it exits. How can I prevent the console from closing when the program is executed through a .bat file?
It turns out the problem was not with freebasic but with the .bat file. I had an exit command at the end of it that caused the console to close.

Thanks.
UEZ
Posts: 988
Joined: May 05, 2017 19:59
Location: Germany

Re: Unwanted closing of console

Post by UEZ »

Sometimes you don't want to pause but pause for x seconds only.

Here the code I'm using for this purpose:

Code: Select all

Dim As String sVer = "v0.60 Build 2020-09-24 by UEZ"
Dim As Double iSeconds = CSng(Command(1))
Dim As Ubyte bInterruptible, bSilent

If Command(2) = "" Then
	bInterruptible = 1
Else
	bInterruptible = Cubyte(Command(2))
Endif
bInterruptible = Iif(bInterruptible > 1, 1, bInterruptible)
If Command(3) = "" Then 
	bSilent = 0
Else
	bSilent = Cubyte(Command(3))
Endif

bSilent = Iif(bSilent > 1, 1, bSilent)

If Command(1) = "" Or iSeconds < 0 Then
	?
	? "Sleep " & sVer
	?
	? "Usage: Sleep [seconds] (interruptable) (silent)"
	?
	? "Seconds must be a number and greater or equal than 0! If 0 then program will pause only."
	? "Interruptible is set to 1 by default. Any key interrupts sleep if interruptible is enabled."
	? "If silent is enabled then no countdown will be printed. Silent is set to 0 by default." 
	?
Else
	If iSeconds = 0 Then
		Sleep
	Else
		Dim As Long iPosCursorX = Lobyte(Locate), iPosCursorY = Hibyte(Locate)
		Dim As Double fCountdown, fStart = Timer()
		Do
			Locate iPosCursorY, Pos
			fCountdown = iSeconds - (Timer - fStart)
			fCountdown = Iif(fCountdown < 0.0, 0.0, fCountdown)
			If bSilent = 0 Then Print Using "Sleeping for ###.## seconds.     "; fCountdown
			If bInterruptible = 1 Then
				If InKey <> "" Then Exit Do
			Endif
			Sleep(5, 1)
		Loop Until (Timer - fStart) >= iSeconds
		If bSilent = 0 Then
		    Locate iPosCursorY, Pos
		    Print Using "Sleeping for ###.## seconds.     "; 0
		Endif
	Endif
Endif
End
Compile it with fbc -s console "Sleep.bas" and add Sleep.exe to your bat / cmd files accordingly.

E.g. Sleep.exe 10 will sleep for 10 seconds before it exits the program. Sleep.exe 0 is pause only.
Post Reply