T3RRYT3RR0R

Batch Multi Threading Example

May 9th, 2020
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 3.34 KB | None | 0 0
  1. ::: ** Batch Multithreading example by T3RRY ** :::
  2.     @Echo Off & CD "%~dp0"                      :::
  3. :::::::::::::::::::::::::::::::::::::::::::::::::::
  4.  
  5. ::: - Test if Thread Two is to be executed
  6.     If /I "%~1" == "Thread2" Goto :Thread2
  7.  
  8. ::: - Thread One Actions
  9.     Mode 160,40
  10.     CHCP 65001 & CLS
  11. ::: { Creates variable Æ = Ascii-27 escape code.
  12. ::: - http://www.dostips.com/forum/viewtopic.php?t=1733
  13. ::: - https://stackoverflow.com/a/34923514/12343998
  14. :::
  15. ::: - Æ (Alt 146) can be used  with and without DelayedExpansion.
  16.     Setlocal
  17.     For /F "tokens=2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  18.         Endlocal
  19.         Set "Æ=%%a"
  20.     )
  21. ::: }
  22.  
  23. ::: - Localise Thread Ones Environment
  24.     Setlocal EnableDelayedExpansion
  25.     If not "!Priority!"=="" (
  26.     Set Priority=High
  27.     Start /B /High "" "%~F0"
  28.     )
  29. ::: - Ensure previous Signal files removed in case program exited incorrectly
  30.         Del /Q /F "%TEMP%\CMDrun.log" > Nul 2> Nul
  31.         Del /Q /F "%TEMP%\CMDend.log" > Nul 2> Nul
  32.     CLS
  33.  
  34. Set "Chars=•☺☻◘○"
  35. :Loop
  36. ::: - Launch Thread Two in Same window if Not already Running
  37. ::: - ** Conditional tests on signal files redirect errors to Standard error to prevent erroneous output **
  38. ::: - when testing occurs at the same time the file is being written to.
  39.     (IF Not Exist "%TEMP%\CMDrun.log" Start /B "" "%~F0" Thread2) 2>Nul
  40. ::: - Animation To indicate commands in thread one are running. This example demonstrates different For loops being
  41. ::: - Executed simultaneously
  42.  
  43.     For /L %%T in (0,1,4) Do (
  44.         Set L1#=0
  45.         For /L %%Y in (2,2,38) Do (
  46.             Set /A L1#+=1
  47.             Set L1[!L1#!]=Echo/
  48.             For /L %%X In (2,2,160) Do (
  49.                 For %%i in (!L1#!) Do Set "L1[!L1#!]=!L1[%%i]!%Æ%[%%Y;%%XH%Æ%!!%%X;%%Y0m!Chars:~%%T,1!"
  50.             )
  51.         For %%E in (!L1#!) Do !L1[%%E]!
  52.         Title THREAD ONE
  53.         )
  54.     rem For /L %%E in (1,1,!L1#!) Do !L1[%%E]!
  55.     )
  56. ::: - Exit Thread One once Thread Two has Completed
  57.     (IF Exist "%TEMP%\CMDend.log" Goto :End_Loop) 2>Nul
  58. Goto :Loop
  59.  
  60. :End_Loop
  61. ::: - Clean up Signal Files
  62.     Echo.
  63.     Del /Q /F "%TEMP%\CMDrun.log"
  64.     Del /Q /F "%TEMP%\CMDend.log"
  65.     Title Example Complete
  66.     Pause >Nul
  67. ::: - Demonstrate stability of multithreading through repetition
  68.     Start "" "%~F0"
  69. Exit
  70.  
  71. :Thread2
  72. TITLE Coloring the faces
  73. ::: Localise Thread Two's Environment
  74.     CHCP 65001 > Nul
  75. ::: { Creates variable Æ = Ascii-27 escape code.
  76. ::: - http://www.dostips.com/forum/viewtopic.php?t=1733
  77. ::: - https://stackoverflow.com/a/34923514/12343998
  78. :::
  79. ::: - Æ (Alt 146) can be used  with and without DelayedExpansion.
  80.     Setlocal
  81.     For /F "tokens=2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  82.         Endlocal
  83.         Set "Æ=%%a"
  84.     )
  85. ::: }
  86. Set "CharSet=/-\-"
  87.     Setlocal EnableDelayedExpansion
  88.     >"%TEMP%\CMDrun.log" (Echo running)
  89.     For /l %%n in (1,1,25) Do (
  90.         For /L %%z in (0,1,3) Do (
  91.             Set L2#=0
  92.             For /L %%a in (39,-2,1) Do (
  93.                 Set /A L2#+=1
  94.                 Set L2[!L2#!]=Echo/
  95.                 For /L %%b In (159,-2,1) Do (
  96.                     For %%i in (!L2#!) Do Set "L2[!L2#!]=!L2[%%i]!%Æ%[%%a;%%bH%Æ%!!%%!!%%!!!!!%%z,1!"
  97.                 )
  98.             For %%v in (!L2#!) Do !L2[%%v]!
  99.             TITLE THREAD TWO
  100.             )
  101.             rem For /L %%c in (1,1,!L2#!) Do !L2[%%c]!
  102.         )
  103.     )
  104.     >"%TEMP%\CMDend.log" (Echo finished)
  105. ::: - Hard exit of Thread Two used to prevent script overflow.
  106. EXIT
Add Comment
Please, Sign In to add comment