Page 1 of 1

Help to break loop

Posted: 02 Feb 2023 21:45
by Dasmius
Sup Guys,

I need a help with my code. I made an Warning Screen where there are some words that blinks and to make this happen I did a loop, But I need when the user hit any key, stop the loop and continue to execute the next commands. I tried a lot of things, like pause >nul , pause /nobreak , some if conditions, but none of them worked. Can you gays have any suggestion?

Here is the code:

@ECHO OFF

MODE CON: COLS=150 LINES=50

CHCP 65001

REM.-- Preparando processador de comandos
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION



set "text1=AO CONFIGURAR O CSO EXTRA"
set "text2=NÃO ALTERE NADA, APENAS"
set "text3=CLIQUE EM NEXT"
set /p "answer=Pressione qualquer tecla para continuar..." < nul
if not "%answer%" == "" goto end


cls
ECHO [1;1H
echo.
echo [1;48;5;9;38;5;11;4mIniciando Configuração Vivo - Tdata[0m
call :progress %count% %total%
:loop
ECHO [4;1H
echo.
echo.
echo [91m**********************************************[0m
echo [91m* *[0m
for /f "tokens=1-5 delims= " %%a in ("%text1%") do (
if !random! equ 1 (
echo [91m* [93m%%a %%b %%c %%d %%e[0m [91m*[0m
) else (
echo [91m* *[0m
)
)
for /f "tokens=1-4 delims= " %%a in ("%text2%") do (
if !random! equ 1 (
echo [91m* [93m%%a %%b %%c %%d[0m [91m*[0m
) else (
echo [91m* *[0m
)
)
for /f "tokens=1-3 delims= " %%a in ("%text3%") do (
if !random! equ 1 (
echo [91m* [93m%%a %%b %%c[0m [91m*[0m
) else (
echo [91m* *[0m
)
)
echo [91m* *[0m
echo [91m**********************************************[0m
echo.
echo.
ping localhost -n 2 > nul
set /a random=%random% %% 2 + 1

goto loop

REM Here goes next commands

I would appreciate any help ans tips, thanks a lot :)

Re: Help to break loop

Posted: 03 Feb 2023 05:38
by T3RRY
Batch file input methods are blocking - that means execution of the batch file waits for input before continuing to the next line

What you need is non blocking input to take input during a loop in the way you desire. Multithreading can be used to achieve this with xcopy - there's a few examples of using this approach that can be found on this forum

Re: Help to break loop

Posted: 03 Feb 2023 12:59
by Dasmius
For now, I just did this workaround while I don't have any idea how to do it properly:

@ECHO OFF

MODE CON: COLS=150 LINES=50

CHCP 65001

REM.-- Preparando processador de comandos
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

SET "text1=AO CONFIGURAR O CSO EXTRA"
SET "text2=NÃO ALTERE NADA, APENAS"
SET "text3=CLIQUE EM NEXT"
SET csocount=6

CLS
ECHO [1;1H
echo [1;48;5;9;38;5;11;4mIniciando Configuração Vivo - Tdata[0m
call :progress %count% %total%

GOTO cso

:cso
ECHO [5;1H
echo.
echo.
echo [91m**********************************************[0m
echo [91m* *[0m
for /f "tokens=1-5 delims= " %%a in ("%text1%") do (
if !random! equ 1 (
echo [91m* [93m%%a %%b %%c %%d %%e[0m [91m*[0m
) else (
echo [91m* *[0m
)
)
for /f "tokens=1-4 delims= " %%a in ("%text2%") do (
if !random! equ 1 (
echo [91m* [93m%%a %%b %%c %%d[0m [91m*[0m
) else (
echo [91m* *[0m
)
)
for /f "tokens=1-3 delims= " %%a in ("%text3%") do (
if !random! equ 1 (
echo [91m* [93m%%a %%b %%c[0m [91m*[0m
) else (
echo [91m* *[0m
)
)
echo [91m* *[0m
echo [91m**********************************************[0m
echo.
echo.
PING localhost -n 2 >nul
set /a random=%random% %% 2 + 1

SET /a csocount=csocount-1

IF %csocount%==0 GOTO exitcso

GOTO cso

:exitcso
CLS
ECHO.
ECHO [1;48;5;9;38;5;11;4mIniciando Configuração Vivo - Tdata[0m
call :progress %count% %total%
ECHO.
ECHO.
ECHO [91m**********************************************[0m
ECHO [91m*[0m [91m*[0m
ECHO [91m*[0m[93m AO CONFIGURAR O CSO EXTRA [0m[91m*[0m
ECHO [91m*[0m[93m NÃO ALTERE NADA, APENAS [0m[91m*[0m
ECHO [91m*[0m[93m CLIQUE EM NEXT [0m[91m*[0m
ECHO [91m*[0m [91m*[0m
ECHO [91m**********************************************[0m
ECHO.
ECHO.
PAUSE

REM NEXT COMANDS GOES HERE

If someone has any better idea, would be nice and wellcome.

Re: Help to break loop

Posted: 04 Feb 2023 09:20
by T3RRY
Dasmius wrote:
03 Feb 2023 12:59
If someone has any better idea, would be nice and wellcome.
This is the bare basics of how to implement a non blocking input method to continue execution of a loop while waiting for a keypress event.

If you wish to break the loop based on another event in addition, you'll need to incorporate the use of a signal file to send a kill command to the second thread.

Code: Select all

@Echo off

	If not "%~1" == "" Goto:%1

	Set "While="
	Start /B /Wait "" "%~f0" XCOPYWAIT >"%~n0.sig" | "%~f0" Anim <"%~n0.sig"
	REM continue desire post animation actions here

Exit /B

:Anim
	<nul Set /P ".=."
	Set /P "While="
	If not defined while Goto:Anim
Exit

:XCOPYWAIT
	for /f "delims=" %%A in ('%SystemRoot%\System32\xcopy.exe /w "%~f0" "%~f0" 2^>nul') do (
		<NUL SET /p "=quit"
	      	EXIT
	)

Re: Help to break loop

Posted: 06 Feb 2023 13:50
by Dasmius
Oh I see!

I'll try it and see what I can do with this.

Thanks Bro :)