我想做的是:我正在从数据库中写一个临时的pdf文件,然后调用这个文件在acrobat阅读器中打开它。是的,pdf是安全的,我自己做的。
现在我的问题是在关闭acrobat阅读器后删除临时文件。这段代码可以工作,但我认为,它并不是真正的最佳实践。
Dim myp As New Process
myp.StartInfo.FileName = filename
myp.Start()
myp.WaitForInputIdle()
myp.WaitForExit()
Dim errorfree As Boolean = False
While errorfree = False
Try
Threading.Thread.Sleep(250)
File.Delete(filename)
errorfree = True
Catch ex As Exception
End Try
End While
myp.Dispose()Info:对于acrobat阅读器,这两行
myp.WaitForInputIdle()
myp.WaitForExit()都不起作用。
发布于 2019-03-20 17:26:28
您可以使用Process.Exited事件:
'creating the process.
Dim myp As New Process
myp.StartInfo.FileName = filename
myp.Start()
'bind the "Exited"-event to a sub.
myp.EnableRaisingEvents = True
AddHandler myp.Exited, AddressOf SubToDeleteFile
'the sub used by the "Exited"-event.
Public Sub SubToDeleteFile(ByVal sender As Object, ByVal e As EventArgs)
Dim errorfree As Boolean = False
While errorfree = False
Try
Dim filename As String = DirectCast(sender, Process).StartInfo.FileName
Threading.Thread.Sleep(250)
File.Delete(filename)
errorfree = True
Catch ex As Exception
End Try
End While
'dispose the process at the end.
If sender IsNot Nothing AndAlso TypeOf sender Is Process Then
DirectCast(sender, Process).Dispose()
End If
End Subhttps://stackoverflow.com/questions/55256936
复制相似问题