Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Waiting For Process To Complete

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
like image 309
Muhnamana Avatar asked Sep 26 '12 13:09

Muhnamana


2 Answers

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
like image 70
Ashley Ross Avatar answered Sep 28 '22 09:09

Ashley Ross


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

like image 36
Aghilas Yakoub Avatar answered Sep 28 '22 10:09

Aghilas Yakoub