【问题标题】:VB.NET Wait a DOS shell program to terminate before continuing- doesn't workVB.NET 在继续之前等待 DOS shell 程序终止 - 不起作用
【发布时间】:2014-04-07 13:18:36
【问题描述】:

我正在 VS2010 上构建一个 Windows 窗体应用程序,我需要通过它执行一个 3d 方 DOS shell 程序(OpenSees.exe),在其中打开一个源文件并执行分析。在此之后,创建了一些输出文件,我需要在我的 VB.NET 应用程序中再次读取这些文件。 问题是OpenSees中的分析可能需要很长时间,所以VB代码必须等待它才能进行。

为此,我尝试了“ShellandWait”子以及“WaitForSingleObject”函数和“进程类”选项,但两者都不起作用。 我的 DOS shell 程序已初始化,但它几乎立即关闭,无法完成分析和创建所需的输出。

这是我使用的代码 sn-ps: 第一次尝试:ShellandWait

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess _
 As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle _
    As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long


Private Sub ShellAndWait(ByVal program_name As String, _
                         Optional ByVal window_style As AppWinStyle = vbNormalFocus, _
                         Optional ByVal max_wait_seconds As Long = 0)
    Dim lngProcessId As Long
    Dim lngProcessHandle As Long
    Dim datStartTime As Date
    Const WAIT_TIMEOUT = &H102
    Const SYNCHRONIZE As Long = &H100000
    Const INFINITE As Long = &HFFFFFFFF

    ' Start the program.
    On Error GoTo ShellError
    lngProcessId = Shell(program_name, window_style)
    On Error GoTo 0
    Threading.Thread.Sleep(1500)
    'System.Windows.Forms.Application.DoEvents()

    SendKeys.Send("source " & filename & ".tcl")
    SendKeys.Send("{ENTER}")

    ' Wait for the program to finish.
    ' Get the process handle.
    lngProcessHandle = OpenProcess(SYNCHRONIZE, 0, lngProcessId)
    If lngProcessHandle <> 0 Then
        datStartTime = Now
        Do
            If WaitForSingleObject(lngProcessHandle, 250) <> WAIT_TIMEOUT Then
                Exit Do
            End If
            'DoEvents()
            If max_wait_seconds > 0 Then
                If DateDiff("s", datStartTime, Now) > max_wait_seconds Then Exit Do
            End If
        Loop
        CloseHandle(lngProcessHandle)
    End If
    Exit Sub

  ShellError:
  End Sub
  ...
  ShellAndWait("OpenSees.exe", , 3)

第二次尝试:ProcessStart

Dim p As New Process
Dim psi As New ProcessStartInfo("OpenSees.exe", "source " & filename & ".tcl")
p.StartInfo = psi
p.Start()

p.WaitForExit()

我不明白为什么这不起作用。任何帮助将不胜感激!

【问题讨论】:

  • 这确实不太可能正常工作。传递opensees.exe 文件和 .tcl 文件的完整 路径名非常重要。完整路径名是 c:\foo\bar\baz.tcl,而不是 baz.tcl
  • @HansPassant 感谢您的直接回复。我认为这不是问题(顺便说一句,我也尝试了完整的路径名,但同样的事情发生了)。在尝试上述代码 sn-ps 之前,我只是利用 Shell 命令执行 OpenSees & SendKeys 命令打开所需文件 & threading.Thread.Sleep 让 VB 代码等待。它有效,但绝对不是一个好的解决方案

标签: vb.net shell process


【解决方案1】:

试试这个:

Shell("OpenSees.exe <arguments>",, True)

【讨论】:

  • 虽然这段代码 sn-p 可以解决问题,但包含解释确实有助于提高帖子的质量。请记住,您正在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。也请尽量不要用解释性 cmets 挤满你的代码,因为这会降低代码和解释的可读性!
猜你喜欢
  • 2021-02-04
  • 2020-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-08
  • 1970-01-01
  • 2020-02-01
  • 2012-04-05
相关资源
最近更新 更多