我有一个用VB.NET (VS2017)构建的小应用程序。为此,目标.NET框架设置为默认值4.6.1。在Windows10的主平台上,一切工作正常。在Windows7专业版的辅助平台上,我看到在退出进程后需要很长时间才能在应用程序中执行其他操作。
我正在调用Internet Explorer窗口,并使用以下命令传递URL:
Process.Start("IExplore.exe", recurl).WaitForExit()
你知道为什么它在Win10下看起来工作得很好,但在Win7中却停顿了很长时间吗?
发布于 2018-01-20 05:15:00
这里有一个建议的扩展方法。它背后的想法是,检查窗口是否打开,然后检查是否关闭。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)
https://stackoverflow.com/questions/48347349
复制相似问题