Process.Start .WaitForExit只在一个平台上暂停长时间停顿?

时间:2018-01-19 18:23:40

标签: .net vb.net visual-studio visual-studio-2017

我有一个用VB.NET(VS2017)构建的小应用程序。目标.NET框架设置为默认值4.6.1。在Windows 10的主要平台上,一切都按预期工作。在Windows 7 Pro的辅助平台上,我看到在退出流程后能够在应用程序中执行其他操作时会有非常长的停顿。

我正在调用Internet Explorer窗口并使用以下命令传递URL:

Process.Start("IExplore.exe", recurl).WaitForExit()

任何想法为什么它似乎在Win10下工作得很好,但在Win7中有一个很长的停顿?

1 个答案:

答案 0 :(得分:0)

这是建议的扩展方法。它背后的想法是你检查窗口是否打开,然后检查它是否关闭。 MainWindowHandle是与窗口指针关联的句柄。

Public Module ProcessExtensionMethods

    <System.Runtime.CompilerServices.Extension()>
    Public Sub WaitForWindowHandleToClose(ByVal p As System.Diagnostics.Process, ByVal pollingInterval As Integer,
                                          ByVal waitForOpenTimeout As Integer, ByVal waitForCloseTimeout As Integer)

        ' Wait for Main Window Handle to Exist
        Dim totalSleep As Integer
        While Not p.HasExited AndAlso p.MainWindowHandle = IntPtr.Zero
            System.Threading.Thread.Sleep(pollingInterval)
            totalSleep += pollingInterval
            p.Refresh()
            If totalSleep >= waitForOpenTimeout And Not waitForOpenTimeout = 0 Then
                Throw New ApplicationException("Waiting too long for process to start")
            End If
        End While

        ' Wait for Main Window Handle to Close
        totalSleep = 0
        While Not p.HasExited AndAlso p.MainWindowHandle.ToInt64 > 0
            System.Threading.Thread.Sleep(pollingInterval)
            totalSleep += pollingInterval
            p.Refresh()
            If totalSleep >= waitForCloseTimeout And Not waitForCloseTimeout = 0 Then
                Throw New ApplicationException("Waiting too long for process to close")
            End If
        End While
    End Sub

End Module

和用法:

    Dim ieProcess As New System.Diagnostics.Process()
    ieProcess.StartInfo.FileName = "iexplore.exe"
    ieProcess.Start()
    ieProcess.WaitForWindowHandleToClose(1000, 20000, 20000)