更有效的等待/倾听过程的方式

时间:2016-01-05 17:21:24

标签: vb.net pdf process

我有一个我一直在使用的工具,但它很受欢迎。它使用sendkeys从另一个应用程序打开pdf。然后我检查pdf是否打开,通过终止进程关闭该pdf,然后再次使用sendkeys转到下一个文档。但是,我发现如果网络和/或文件大小之间存在任何延迟时间,程序就不会检测到该过程,程序也会停止运行。是否有更好的方式来倾听流程?

Sub ProcessPDF()
        Dim z As Integer = Next1
        AppActivate("Hyperspace")
        For i = 1 To z          
            SendKeys.SendWait("{ENTER}")
            p = Process.GetProcessesByName("Acrobat")
            If p.Count > 0 Then
                p(0).CloseMainWindow()      
                SendKeys.SendWait("{LEFT}")
                SendKeys.SendWait("^Q")
                SendKeys.SendWait("{DOWN}")
            Else
                label2.Text = "An error has occurred! Please try again."
                MsgBox("An error has occurred! Please try again.")
            End If
            Next1 = Next1 - 1
            label2.Text = Next1 & " PDFs Left!"
        Next
End Sub

1 个答案:

答案 0 :(得分:0)

方式1:

wait:
For Each p In Process.GetProcesses
    If p.ProcessName = ("Acrobat") Then
         Threading.Thread.Sleep(1000)
         GoTo wait
    End If
Next

Way2:

Private Function Run(ByVal process As String, ByVal parameters As String) As String
                Dim psi As ProcessStartInfo = New ProcessStartInfo(process)
                psi.Arguments = "your commands"
                psi.RedirectStandardError = True
                psi.UseShellExecute = False
                Dim output As String = String.Empty
                Dim proc As Process = System.Diagnostics.Process.Start(psi)
                proc.WaitForExit()
                Dim outputStream As StreamReader = proc.StandardError
                output = outputStream.ReadToEnd
                proc.Close()
                Return output
End Function

强制杀戮:

waitkill:
        For Each p In Process.GetProcesses
            If p.ProcessName = ("Acrobat") Then
                p.Kill()
                GoTo waitkill
            End If
        Next