vb.net:vb.net等待外部程序运行完毕才继续



上次我已介绍如何运行外部,今天我继续讲这话题:
1.有好多时,我们需要外部EXE,并且要等它运行完毕,我们才可以继续下面动作,那我们怎样去实现了,请看以下代码.
        \'怎样等待外部运行完毕.
        \'从系统资料夹读入文件
        Dim sysFolder As String = _
                    Environment.GetFolderPath(Environment.SpecialFolder.)
        \'创建个新进程结构
        Dim pInfo As New ProcessStartInfo
        \'设置其成员FileName为系统资料Eula.txt
        pInfo.FileName = sysFolder & \"\\eula.txt\"
        \'运行该文件
        Dim p As Process = Process.Start(pInfo)
        \'等待装载完成
        p.WaitForInputIdle
        \'等待进行程退出
        p.WaitForExit
        \'继续执行下面代码
        MessageBox.Show(\"继续执行代码\")


2.我们想在5秒钟后,强行关闭它.而不是需要我手工关闭.
    \'设置退出时间
    Dim timeOut As Integer = 5000
    Dim sysFolder As String = _
         Environment.GetFolderPath(Environment.SpecialFolder.)
    Dim pInfo As New ProcessStartInfo
    pInfo.FileName = sysFolder & \"\\eula.txt\"
    Dim p As Process = Process.Start(pInfo)
    p.WaitForInputIdle
    p.WaitForExit(timeOut)
    \'检查是否在超时前已关闭了.
    If p.HasExited = False Then
        \'进行程还在运行
        \'看进程有没有回应
        If p.Responding Then
            p.CloseMainWindow \'关闭窗口
        Else
            p.Kill  \'强行中断
        End If
    End If
    MessageBox.Show(\"继续执行代码\")
Tags:  vb.net源码 vb.net语法 vb.net教程 vb.net

延伸阅读

最新评论

发表评论