使用Shell运行一个外部程序后,如何让VB程序中断Shell命令后面的代码,等调用的那个外部程序运行完后再继续运行,就像运行外部程序如打开一个模式窗体一样。关键在于如何得知调用的外部程序已运行完毕!使用API???

解决方案 »

  1.   

    'ShellWait示例
    '先点击command3运行Notepad.exe
    '然后再点点Command1
    '如果在10秒之内关闭Notepad,则弹出提示框0
    '如果10秒内未关闭Notepad.则弹出提示框258'in a Form
    Option ExplicitDim ProcessID As LongPrivate Sub Command1_Click()
    Dim WaitHandle As Long
    Dim Ret As Long
        WaitHandle = OpenProcess(SYNCHRONIZE, True, ProcessID)
        Ret = WaitForSingleObject(WaitHandle, 3000)
        CloseHandle WaitHandle
        MsgBox Ret
    End SubPrivate Sub Command3_Click()
        ProcessID = Shell("notepad.exe")
    End Sub'in a moduleOption ExplicitPublic Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As LongPublic Const SYNCHRONIZE = &H100000'///////////////////////////////////////////
    '函数名称:ShellWait( Process , [Timeout] )
    '函数功能:等待程序运行结束
    '参数说明:
    '   [ProcessID]:程序进程ID(Shell函数返回值)
    '   [Timeout]:超时时间(单位:毫秒),缺省为20秒
    '返回值(Long型):
    '   [258]超时(进程未结束)
    '   [0]进程已结束
    '   [-1]不存在的进程
    '///////////////////////////////////////////
    Public Function ShellWait(ProcessID As Long, Optional Timeout As Long = 20000) As Long
    Dim WaitHandle As Long
        WaitHandle = OpenProcess(SYNCHRONIZE, True, ProcessID)
        ShellWait = WaitForSingleObject(WaitHandle, Timeout)
        CloseHandle WaitHandle
    End Function