首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

判断进程结束和多进程有关问题

2012-02-16 
判断进程结束和多进程问题我在vb程序中调用shell函数,执行.bat批处理文件现在知道利用API可以判断一个进程

判断进程结束和多进程问题
我在vb程序中调用shell函数,执行.bat批处理文件
现在知道利用API   可以判断一个进程是否结束,如下
Const   SYNCHRONIZE   =   &H100000
Const   INFINITE   =   &HFFFFFFFF
Private   Declare   Function   OpenProcess   Lib   "kernel32 "   (ByVal   dwDesiredAccess   As   Long,   ByVal   bInheritHandle   As   Long,   ByVal   dwProcessId   As   Long)   As   Long
Private   Declare   Function   CloseHandle   Lib   "kernel32 "   (ByVal   hObject   As   Long)   As   Long
Private   Declare   Function   WaitForSingleObject   Lib   "kernel32 "   (ByVal   hHandle   As   Long,   ByVal   dwMilliseconds   As   Long)   As   Long

但是调用了WaitForSingleObject后,vb中的其他程序都不能运行了,我可以同时判断进程是否结束,同时也能在vb中做其他操作呢?
我不知道这个是不是和多进程有关,请高手指点一下吧

[解决办法]
WaitForSingleObject本来就是是阻塞的
只有当其返回时才会接着执行下面语句
用多线程可以解决

DoEvent
[解决办法]
你为什么不用Shell来调用外部应用程序呢,
然后在定时器中用“GetExitCodeProcess”api来检测调用的外部应用程序(就是cmd.exe)是否结束了运行。
示例如下
Private Declare Function OpenProcess Lib "Kernel32 " (ByVal dwDesiredAccess _
As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "Kernel32 " _
(ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function GetExitCodeProcess Lib "Kernel32 " _
(ByVal hProcess As Long, lpExitcode As Long) As Long
Private Declare Function CloseHandle Lib "Kernel32 " (ByVal hobject As Long) As Long

Private RetProcess As Double
Private Const PROCESS_QUERY_INFORMATION = &H400
Private Const STATUS_PENDING = &H103&
Private hProcess As Long, Exitcode As Long

RetProcess = Shell( "cmd.exe d:\user.bat ", vbNormalFocus)
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, RetProcess)

'用定时器检测进程是否退出
Private Sub TimerProcess_Timer()
TimerProcess.Enabled = False
Call GetExitCodeProcess(hProcess, Exitcode)
If Exitcode <> STATUS_PENDING Then
'进程已关闭
Call CloseHandle(hProcess)
TimerProcess.Enabled = False
Else
'进程未关闭
TimerProcess.Enabled = True
End If
End Sub
[解决办法]
cmd.exe win2k和xp下的命令行控制台,在cmd.exe下运行user.bat

热点排行