等待Acrobat Reader退出

时间:2019-03-20 09:00:53

标签: vb.net

我想做什么:我正在从数据库中写入一个临时pdf文件,并调用该文件在acrobat Reader中打开它。是的,pdf是安全的,我是自己制作的。

现在我的问题是在关闭Acrobat Reader之后删除临时文件。这段代码有效,但是我认为这并不是最好的做法。

    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()

信息:对于Acrobat Reader,这两行

 myp.WaitForInputIdle()
 myp.WaitForExit()

不起作用。

1 个答案:

答案 0 :(得分:1)

您可以使用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 Sub