上次我已介绍如何运行外部程序,今天我继续讲这一话题:
1.有好多时,我们需要调用外部的exe程序,并且要等它运行完毕,我们才可以继续下面的动作,那我们怎样去实现了,请看以下代码.
怎样等待外部程序运行完毕.
从系统资料夹读入文件
dim sysfolder as string = _
environment.getfolderpath(environment.specialfolder.system)
创建一个新的进程结构
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.system)
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("继续执行代码")
