等待过程完成

Muh*_*ana 2 vb.net process visual-studio-2010

有没有办法暂停进程或等待unitl进程完成后再继续下一行代码?

这是我当前压缩所有PDF然后删除的过程.目前,它在压缩之前删除文件已完成.有没有办法暂停/等待过程完成?

    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
Run Code Online (Sandbox Code Playgroud)

Ash*_*oss 12

Process.Start返回一个Process实例.正如其他人所提到的,你可以使用WaitForExit()方法,虽然你应该使用WaitForExit(Integer),其中包括一个超时,以防万一与压缩过程出现问题.

所以你的代码会变成:

...
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
Run Code Online (Sandbox Code Playgroud)


Agh*_*oub 8

你可以使用process.WaitForExit方法

WaitForExit可以使当前线程等待,直到相关进程退出.

链接:http://msdn.microsoft.com/fr-fr/library/system.diagnostics.process.waitforexit(v = vs.80).aspx