首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >等待Acrobat Reader退出

等待Acrobat Reader退出
EN

Stack Overflow用户
提问于 2019-03-20 17:00:53
回答 1查看 80关注 0票数 1

我想做的是:我正在从数据库中写一个临时的pdf文件,然后调用这个文件在acrobat阅读器中打开它。是的,pdf是安全的,我自己做的。

现在我的问题是在关闭acrobat阅读器后删除临时文件。这段代码可以工作,但我认为,它并不是真正的最佳实践。

代码语言:javascript
复制
    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阅读器,这两行

代码语言:javascript
复制
 myp.WaitForInputIdle()
 myp.WaitForExit()

都不起作用。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-20 17:26:28

您可以使用Process.Exited事件:

代码语言:javascript
复制
'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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55256936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档