Waiting For Process To Complete
Solution 1
You can use process.WaitForExit
method
WaitForExit can make the current thread wait until the associated process to exit.
Link : http://msdn.microsoft.com/fr-fr/library/system.diagnostics.process.waitforexit(v=vs.80).aspx
Solution 2
Process.Start returns a Process instance. As others have mentioned, you can use the WaitForExit() method, although you should probably use WaitForExit(Integer), which includes a timeout for just in case something goes wrong with the zipping process.
So your code would become something like:
...
Dim zipper As System.Diagnostics.Process = System.Diagnostics.Process.Start(psInfo)
Dim timeout As Integer = 60000 '1 minute in milliseconds
If Not zipper.WaitForExit(timeout) Then
'Something went wrong with the zipping process; we waited longer than a minute
Else
'delete remaining pdfs
...
End If
Solution 3
There are several WaitForExit methods available.
Check out Process.WaitForExit.
WaitForExit()
makes the current thread wait until the associated process terminates. It should be called after all other methods are called on the process. To avoid blocking the current thread, use the Exited event.
- Instructs the Process component to wait indefinitely for the associated process to exit.
WaitForExit(Int32)
makes the current thread wait until the associated process terminates. It should be called after all other methods are called on the process. To avoid blocking the current thread, use the Exited event.
- Instructs the Process component to wait the specified number of milliseconds for the associated process to exit.
Muhnamana
Phillies, Flyers, Eagles, Tech & video game junkie. My experience is mainly in .NET (VB and my goal is to move into a C# environment) with some VBA experience as well.
Updated on March 14, 2020Comments
-
Muhnamana over 4 years
Is there any way to pause a process or wait unitl the process is complete before continuing onto the next line of code?
Here is my current process to zip all PDFs and then delete. Currently, its deleting files before the zipping is complete. Is there a way to pause/wait until the process is complete?
Dim psInfo As New System.Diagnostics.ProcessStartInfo("C:\Program Files\7-Zip\7z.exe ", Arg1 + ZipFileName + PathToPDFs) psInfo.WindowStyle = ProcessWindowStyle.Hidden System.Diagnostics.Process.Start(psInfo) 'delete remaining pdfs For Each foundFile As String In My.Computer.FileSystem.GetFiles("C:\Temp\", FileIO.SearchOption.SearchAllSubDirectories, "*.pdf") File.Delete(foundFile) Next