vb.net 等待进程完成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12603279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Waiting For Process To Complete
提问by Muhnamana
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?
这是我当前压缩所有 PDF 然后删除的过程。目前,它在压缩完成之前删除文件。有没有办法暂停/等待过程完成?
Dim psInfo As New System.Diagnostics.ProcessStartInfo("C:\Program Files-Zipz.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
回答by Ashley Ross
Process.Startreturns a Processinstance. 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.
Process.Start返回一个Process实例。正如其他人提到的,您可以使用WaitForExit()方法,尽管您可能应该使用WaitForExit(Integer),它包括超时,以防万一压缩过程出现问题。
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
回答by Aghilas Yakoub
You can use process.WaitForExit
method
你可以使用process.WaitForExit
方法
WaitForExit can make the current thread wait until the associated process to exit.
WaitForExit 可以使当前线程等待关联进程退出。
Link : http://msdn.microsoft.com/fr-fr/library/system.diagnostics.process.waitforexit(v=vs.80).aspx
链接:http: //msdn.microsoft.com/fr-fr/library/system.diagnostics.process.waitforexit(v=vs.80).aspx
回答by Richard Chambers
There are several WaitForExit methods available.
有多种 WaitForExit 方法可用。
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 Exitedevent.
- 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.
WaitForExit()
使当前线程等待直到相关进程终止。它应该在进程上调用所有其他方法之后调用。为避免阻塞当前线程,请使用 Exited事件。
- 指示 Process 组件无限期地等待关联的进程退出。
WaitForExit(Int32)
使当前线程等待相关进程终止。它应该在进程上调用所有其他方法之后调用。为避免阻塞当前线程,请使用 Exited 事件。
- 指示 Process 组件等待指定的毫秒数,以便关联的进程退出。