views:

311

answers:

2

I'm using a pre-build event in Visual Studio to run a batch (.bat) file that performs some code generation (specifically, I'm running SqlMetal.exe to generate LinqToSql code).

Is the batch file guaranteed to finish before the compilation begins? Or does it run the batch asynchronously?

Bottom line: I want to make sure the new code gets compiled, not the old code.

If it's not guaranteed -- what solutions are available?

+3  A: 

Yes. But note that if you have more than one step in your pre-build, only the last step will be checked for errors. See Gotcha! Visual Studio Pre/Post-Build Events for more info.

Duncan Bayne
Thanks and thank for the tip! +1 There seems to be a little disagreement here, though. Take a look at EricLaw's comment (above).
DanM
We're both right :-) If you call a batch file that itself spawns asynch processes, then the batch file itself is guaranteed to complete, but the asynch processes are not. If you want your batch file to wait for spawned processes, use "start /wait".
Duncan Bayne
Ahh, thx :) I don't believe I have any async processes. Is using `start /wait` equivalent to not using `start` at all? I'm basically just running a couple .exe's that take a few seconds and doing a couple rename and move operations.
DanM
start /wait is almost the same as using no start at all. However, it opens a new window for the spawned process. If that is not required, then you can just omit it.
Tarydon
Thanks for all your help!
DanM
+1  A: 

The batch file will complete only after the process it has started has itself completed, unless you use the start command in the batch file. As a simple test, you could just create a batch file like this:

notepad.exe

And set that as your pre-build event. You will see that the build starts only after you shut down notepad.

Tarydon
+1 Thanks, I don't I use the `start` command in any of my batches.
DanM