Solved Combining batch scripts (asynchronous calls(?))

Various brands / CUSTOM BUILT
June 7, 2015 at 18:42:30
Specs: Windows 7 Professional x64, 3.6GHz 16GB
One script has this loop:
@Echo off
:loop
cls
Echo Wait
timeout /nobreak /t 1 > nul
cls
Echo Wait.
timeout /nobreak /t 1 > nul
cls
Echo Wait..
timeout /nobreak /t 1 > nul
cls
Echo Wait...
timeout /nobreak /t 1 > nul
goto loop

is it possible to have this loop running while another script (in the same session) is performing some other, more time consuming operations?

99 little bugs in the code,
99 little bugs.
Take one down, patch it around,
129 little bugs in the code.


See More: Combining batch scripts (asynchronous calls(?))

Report •

✔ Best Answer
June 9, 2015 at 08:26:10
No. Short of a debugger, no one can. That's kinda the point of these things. Again, keeping with the theoretical, I'd probably have the child process create a second temp file, and have it put its results in that file. Then it'd be up to the main program to read/parse that second file, and clean up once it was done.

If what you're doing can be done with asynchronous calls in VBScript / JScript, you don't have to worry about modifying a different process' memory, as everything could be done from the main program.

How To Ask Questions The Smart Way



#1
June 7, 2015 at 22:17:46

What do you really want to do with this? What is the point? Without more details of what you want to do, I (for one) cannot make a good test. As far as you've taken it, my initial answer is no, but don't quote me on that! From my understanding, any two processes running in the "same session" must be hierarchical: A parent spawns a child process, and nothing is communicated until the child process terminates. (sad!). "START" initiates another session. All that matters is what you want to achieve, and that is still a mystery (to me).

Report •

#2
June 8, 2015 at 01:33:55
Sorry if it was a bit unclear.

Imagine a loading screen, the first script runs to get a smooth visual until the main script is complete (The visual loop/script terminates when the main script is complete)

I got the idea from Unity3D, where you can do loading asynchronously, like you can have a subroutine/script running WHILE the main loading script does its thing.

The important thing is that the timeout /nobreak /t 1 commands should be lasting for exactly that one second. Since some batch commands take longer to process than others, I imagined that I could have two scripts running asynchronously (the one with the timeouts visible and the main script hidden/invisible, possibly in the same session/window, the main script does not write to screen)

99 little bugs in the code,
99 little bugs.
Take one down, patch it around,
129 little bugs in the code.

message edited by RainBawZ


Report •

#3
June 8, 2015 at 11:05:34
Right, never felt the need to do something like this with batch scripting, so I'm going to stick with theoretical.

You can run background tasks with START /B, which will keep everything contained to a single window. If the user closes the window, all background processes will quit as well, so that's nice.

The biggest problem is signaling. How does the "background" job tell the "foreground" script it's done processing? Probably the "best" way would be to create a temp file, pass the name of the temp file to the "background" job, and have the background job delete the temp file as its last action. Then you're just checking for the existence of a file, which is simple to do in batch.

The real answer is to use a better language. VBScript/JScript could potentially do what you want asynchronously, depending on what you're trying to do and how you go about it.

PowerShell has actual background jobs, so all of the signaling and management is done for you.

Any programming language with access to the OS of choice's thread support can do this as well.

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
June 8, 2015 at 18:42:32
It'd be the best to stick with batch, as the main script sets a big amount of variables and runs through a lot of subroutines for checking data and files. I don't know how VBS works with batch etc. (If VBS/Js can set variables that can be used in the batch file, then I guess it's okay, but it would be difficult for me to make additional changes and tweaks when I don't know how the languages work). I assume if there was a way to CALL the main script then the variables would be set within the same environment.

Would START /B set the variables in the same environment as the one it was started from, or would they be set locally just for that instance?

99 little bugs in the code,
99 little bugs.
Take one down, patch it around,
129 little bugs in the code.


Report •

#5
June 8, 2015 at 21:31:28
Using START /B to run a batch file spawns a new instance of CMD, so the standard rules of inheritance applies. Namely, the child process would get an independent copy of the parent's environment block, so there's no updating values in the parent's environment. There are various hacks to get around the boundaries of SETLOCAL, so you might try those, but expect to meet with limited success.

How To Ask Questions The Smart Way


Report •

#6
June 9, 2015 at 06:08:17
Would VBScript/JScript be able to update values in the parent environment?

99 little bugs in the code,
99 little bugs.
Take one down, patch it around,
129 little bugs in the code.


Report •

#7
June 9, 2015 at 08:26:10
✔ Best Answer
No. Short of a debugger, no one can. That's kinda the point of these things. Again, keeping with the theoretical, I'd probably have the child process create a second temp file, and have it put its results in that file. Then it'd be up to the main program to read/parse that second file, and clean up once it was done.

If what you're doing can be done with asynchronous calls in VBScript / JScript, you don't have to worry about modifying a different process' memory, as everything could be done from the main program.

How To Ask Questions The Smart Way


Report •

Ask Question