Utility to wait for a process - Wait.exe
As I mentioned in this post, I spent a bunch of time looking for a command line utility that would wait until a specific process has completed. I was dumbfounded that I couldn’t find one, so I set out to write one. I chose C# because all the tools are available. Here is the usage text:
wait <process_name> [-t:secs] [-v]
wait -p[id] <process_id> [-t:secs] [-v]
wait -m[odule] <moduleFileName> [-t:secs] [-v]
wait -t secs
Wait for a process to exit or for a specified amount of time.
where:
process_name - Name of the process to wait for.
process_id - ID of the process to wait for.
moduleFileName - File name of the module (i.e. image file) running in the
process to wait for. The first process that matches will
be the one that is waited for. If no extension is
specified, .exe will be used.
secs - Maximum number of seconds to wait.
-v - Display information about the process before waiting.
It’s pretty straightforward. It uses several techniques to find the right process using the System.Threading.Process class.
- If a process ID is specified, there is exactly one process that will match. The
Process.GetProcessById()static method is used to find the process in this case. - If a process name is specified, the
Process.GetProcessByName()static method is used. This method can return multiple processes, in which case an error is returned. - If a module (e.g. executable image file name) is specified, Wait.exe loops through all processes until it finds a process that matches. The first one that is found is considered a match.
Once the process is found, the WaitForExit() method is called on the Process object. An optional timeout value can also be specified for the maximum number of seconds to wait. Finally, if only a timeout value is specified, Wait.exe will simply call the System.Threading.Thread.Sleep() static method and then return, allowing timed waits to be added to command scripts.
Room for improvement
This utility is far from perfect, but I don’t have a reason at the moment to address its deficiencies. Here are some wishlist items. Please add a comment if you’d like me to address any of these.
- Detect multiple processes that have the same module loaded.
- Allow multiple processes to be waited on.
- Sign the assembly image so that it can be run across a network.
- Convert to C++ to produce a smaller image (it’s currently 24Kb).
Resources
Wait-Source.zip (5.8 KiB, 1,324 hits)
Wait-Executable.zip (4.9 KiB, 1,718 hits)


- while this solution is a whole lot simpler, I was doing everything in my solution that it does.

