Multi-theading in batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Multi-theading in batch

#1 Post by Adrianvdh » 27 Apr 2015 14:30

Hi all, i've been a way for a while but I'm back :)

I wanted to know if I could run multiple functions (processes) on my single batch file. I found a simple technique to do this and it works for running multiple infinite loops at the same time, which is nice.
I played around and found making it run a little more complex it ran into some issues and didn't work after all.

Code: Select all

@echo off

if not "%1" == "" goto :%1

start /b %~nx0 2
start /b %~nx0 3

:1
echo Thread 1 Loop
ping localhost -n 2 >nul
goto :1

:2
echo Thread 2 Loop
ping localhost -n 2 >nul
goto :2

:3
echo Thread 3 Loop
ping localhost -n 2 >nul
goto :3
[\code]

The simple multi-theading code is above and does work as I explained earlier.

But I wanted something more 'stable', (1) for example the file name doesn't work with spaces:

"C:\Users\Adrian\Desktop\multi theading\multi.bat" - doesn't work.

"C:\Users\Adrian\Desktop\multitheading\multi.bat" - works.

(2) How do I get it to support multiple arguments?

[code]
if not "%1" == "" echo(

start /b %~nx0 checkcalcDurationReg /d /r
echo Running simulation
pause

:checkcalcDurationReg
echo Running new process
set "/d="&set "/r="
for %%a in (%*) do (
  if "%%~a"=="/d" ( set "/d=true"
   ) else if "%%~a"=="/r" set "/r=true"
)
if defined /d set calcLoadingDurationmode=Enabled
if defined /r (
reg query "%RegKey%" /v "CALC_LOADING_DURATION" >nul 2>nul
if "!errorlevel!"=="0" for /f "tokens=2*" %%i in ('reg query "%RegKey%" /v "CALC_LOADING_DURATION"') do set "calcLoadingDurationmode=%%j"
if "%calcLoadingDurationmode%"=="Enabled" (exit /b
) else if "%calcLoadingDurationmode%"=="Disabled" (exit /b
) else ( >nul reg delete "%RegKey%" /v "CALC_LOADING_DURATION" /f )
call :checkcalcDurationReg /d
>nul reg add "%RegKey%" /v "CALC_LOADING_DURATION" /t REG_SZ /d "%calcLoadingDurationmode%" /f )
echo end of process
exit /b


I need a way (if possible) to run mulitple threads within one batch file with supportive file name directories and to call parameters for a function, within the same batch file.

If anyone could help me out here that would be great!
Kind regards,
Adrian :)

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Multi-theading in batch

#2 Post by dbenham » 27 Apr 2015 15:23

You should EXIT /B after your main routine - before your sub-process routines

When using START with a command that must be quoted, you must add a quoted title as the first argument. An empty string "" works just fine.

Code: Select all

@echo off
if "%~1" equ "launch" shift /1&shift /1&goto %2

:: Main Script
start /b "" "%~f0" launch routine1 4 arg1 arg2
start /b "" "%~f0" launch routine1 3 argA argB
start /b "" "%~f0" launch routine2 2 Hello
start /b "" "%~f0" launch routine2 1 Goodbye
ping localhost -n 5 >nul
echo End of main routine
exit /b

:routine1
ping localhost -n %1 >nul
echo End routine1: echo %%1=%1 %%2=%2 %%3=%3
exit

:routine2
ping localhost -n %1 >nul
echo End routine2: echo %%1=%1 %%2=%2
exit


Dave Benham

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: Multi-theading in batch

#3 Post by Adrianvdh » 28 Apr 2015 00:41

Hi dbenham, thank you for replying and posting your solution, I really like it.

Please could you break down what happens line by line?

Thank you.

Adrian

jwoegerbauer
Posts: 33
Joined: 01 Jan 2013 12:09

Re: Multi-theading in batch

#4 Post by jwoegerbauer » 28 Apr 2015 02:21

Yes, starting multiple threads from within a batch file is possible, but of course this only is true, if the apps you start are different ones or, if the app you start is always the same, if it allows running multiple instances of itself. For example HOBOCOPY doesn't allow running multiple instances of itself.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Multi-theading in batch

#5 Post by Aacini » 28 Apr 2015 10:01

See this topic.

Antonio

Post Reply