首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是否仅在单一平台上使用Process.Start .WaitForExit长时间暂停?

是否仅在单一平台上使用Process.Start .WaitForExit长时间暂停?
EN

Stack Overflow用户
提问于 2018-01-20 02:23:40
回答 1查看 530关注 0票数 0

我有一个用VB.NET (VS2017)构建的小应用程序。为此,目标.NET框架设置为默认值4.6.1。在Windows10的主平台上,一切工作正常。在Windows7专业版的辅助平台上,我看到在退出进程后需要很长时间才能在应用程序中执行其他操作。

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

代码语言:javascript
运行
复制
Process.Start("IExplore.exe", recurl).WaitForExit()

你知道为什么它在Win10下看起来工作得很好,但在Win7中却停顿了很长时间吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-20 05:15:00

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

代码语言:javascript
运行
复制
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

和用法:

代码语言:javascript
运行
复制
    Dim ieProcess As New System.Diagnostics.Process()
    ieProcess.StartInfo.FileName = "iexplore.exe"
    ieProcess.Start()
    ieProcess.WaitForWindowHandleToClose(1000, 20000, 20000)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48347349

复制
相关文章

相似问题

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