Wait until a process ends
I think you just want this:
var process = Process.Start(...);
process.WaitForExit();
See the MSDN page for the method. It also has an overload where you can specify the timeout, so you're not potentially waiting forever.
Wait for process to finish, or user input
Use kill -0
to validate that the process is still there and read
with a timeout of 0 to test for user input. Something like this?
pid=$!
while kill -0 $pid; do
read -t 0 && exit
sleep 1
done
wait one process to finish and execute another process
You can achieve a simple way of process synchronization in bash
using wait
which waits for one or more number of background jobs to complete before running the next.
You generally run jobs in the background by appending the &
operator to the end of a command. At that point the PID
(process ID) of the newly created background process is stored in a special bash variable: $!
and wait
command allows this process to be terminate before running the next instruction.
This can be demonstrated by a simple example
$ cat mywaitscript.sh
#!/bin/bash
sleep 3 &
wait $! # Can also be stored in a variable as pid=$!
# Waits until the process 'sleep 3' is completed. Here the wait on a single process is done by capturing its process id
echo "I am waking up"
sleep 4 &
sleep 5 &
wait # Without specifying the id, just 'wait' waits until all jobs started on the background is complete.
echo "I woke up again"
Command ouput
$ time ./mywaitscript.sh
I am waking up
I woke up again
real 0m8.012s
user 0m0.004s
sys 0m0.006s
You can see the script has taken ~8s to run to completion. The breakdown on the time is
sleep 3
will take full 3s to complete its executionsleep 4
andsleep 5
are both started sequentially one after next and it has taken the max(4,5) which is approximately ~5s to run.
You can apply the similar logic to your question above. Hope this answers your question.
Node.JS: how to wait for a process to finish before continuing?
downloadAndSaveFiles
returns a promise (because the function is async
) but that promise doesn't "wait" for https.get
or fs.createWriteStream
to finish, and therefore none of the code that calls downloadAndSaveFiles
can properly "wait".
If you interact with callback APIs you cannot really use async/await
. You have to create the promise manually. For example:
const downloadAndSaveFiles = ({ url, dir }) => {
return new Promise((resolve, reject) => {
// TODO: Error handling
https.get(url, (res) => {
// File will be stored at this path
console.log('dir: ', dir);
var filePath = fs.createWriteStream(dir);
filePath.on('finish', () => {
filePath.close();
console.log('Download Completed');
resolve(); // resolve promise once everything is done
});
res.pipe(filePath);
});
});
};
Wait for process to finish before proceeding in Java
Call Process#waitFor()
. Its Javadoc says:
Causes the current thread to wait, if necessary, until the process represented by this
Process
object has terminated.
Bonus: you get the exit value of the subprocess. So you can check whether it exited successfully with code 0
, or whether an error occured (non-zero exit code).
Related Topics
How to Load an Assembly to Appdomain with All References Recursively
Create Folder and File on Current User Profile, from Admin Profile
C# Reflection: How to Get Class Reference from String
How to Change the Timeout on a .Net Webclient Object
C# Linq Group by on Multiple Columns
ASP.NET MVC $.Post Call Returning String...Need Help with Format for Jqgrid
How to Bind a Tabcontrol to a Collection of Viewmodels
Differencebetween 'Protected' and 'Protected Internal'
How to Detect If Console.In (Stdin) Has Been Redirected
Save and Load Memorystream To/From a File
How to Programmatically Generate Keypress Events in C#
How Using Try Catch for Exception Handling Is Best Practice
Change Wpf Controls from a Non-Main Thread Using Dispatcher.Invoke
How to Serialize a String as Cdata Using Xmlserializer
Automatically Create an Enum Based on Values in a Database Lookup Table
How to Serialize a Dictionary as Part of Its Parent Object Using JSON.Net