请教:在VB中,使用shell调用了一个批处理,如何判断是否执行完成

derbywgz 2009-08-06 05:02:11
在VB当中,使用shell调用了两个bat文件

需要在第一个bat文件执行完成后,执行第二个

目前的情况是,第一个执行了,没有执行完成,就开始执行第二个了,导致出现结果不正确

请高手赐教,谢谢!!
...全文
596 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
evilteam 2009-10-14
  • 打赏
  • 举报
回复
定时器的方法不好!
一楼的最科学了····
derbywgz 2009-08-07
  • 打赏
  • 举报
回复
多谢各位,我使用了最简单的,定时器方式
lqnoway 2009-08-06
  • 打赏
  • 举报
回复
以下是老外写的一个类,用来获得dos命令的执行结果。


Option Explicit
'DOSOutputs.cls
'The CreatePipe function creates an anonymous pipe,
'and returns handles to the read and write ends of the pipe.
Private Declare Function CreatePipe Lib "kernel32" ( _
phReadPipe As Long, _
phWritePipe As Long, _
lpPipeAttributes As Any, _
ByVal nSize As Long) As Long

'Used to read the the pipe filled by the process create
'with the CretaProcessA function
Private Declare Function ReadFile Lib "kernel32" ( _
ByVal hFile As Long, _
ByVal lpBuffer As String, _
ByVal nNumberOfBytesToRead As Long, _
lpNumberOfBytesRead As Long, _
ByVal lpOverlapped As Any) As Long

'Structure used by the CreateProcessA function
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type

'Structure used by the CreateProcessA function
Private Type STARTUPINFO
cb As Long
lpReserved As Long
lpDesktop As Long
lpTitle As Long
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type

'Structure used by the CreateProcessA function
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type

'This function launch the the commend and return the relative process
'into the PRECESS_INFORMATION structure
Private Declare Function CreateProcessA Lib "kernel32" ( _
ByVal lpApplicationName As Long, _
ByVal lpCommandLine As String, _
lpProcessAttributes As SECURITY_ATTRIBUTES, _
lpThreadAttributes As SECURITY_ATTRIBUTES, _
ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, _
ByVal lpCurrentDirectory As Long, _
lpStartupInfo As STARTUPINFO, _
lpProcessInformation As PROCESS_INFORMATION) As Long

'Close opened handle
Private Declare Function CloseHandle Lib "kernel32" ( _
ByVal hHandle As Long) As Long

'Consts for the above functions
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const STARTF_USESTDHANDLES = &H100&
Private Const STARTF_USESHOWWINDOW = &H1


Private mCommand As String 'Private variable for the CommandLine property
Private mOutputs As String 'Private variable for the ReadOnly Outputs property

'Event that notify the temporary buffer to the object
Public Event ReceiveOutputs(CommandOutputs As String)

'This property set and get the DOS command line
'It's possible to set this property directly from the
'parameter of the ExecuteCommand method
Public Property Let CommandLine(DOSCommand As String)
mCommand = DOSCommand
End Property

Public Property Get CommandLine() As String
CommandLine = mCommand
End Property

'This property ReadOnly get the complete output after
'a command execution
Public Property Get Outputs()
Outputs = mOutputs
End Property

Public Function ExecuteCommand(Optional CommandLine As String) As String
Dim proc As PROCESS_INFORMATION 'Process info filled by CreateProcessA
Dim ret As Long 'long variable for get the return value of the
'API functions
Dim start As STARTUPINFO 'StartUp Info passed to the CreateProceeeA
'function
Dim sa As SECURITY_ATTRIBUTES 'Security Attributes passeed to the
'CreateProcessA function
Dim hReadPipe As Long 'Read Pipe handle created by CreatePipe
Dim hWritePipe As Long 'Write Pite handle created by CreatePipe
Dim lngBytesread As Long 'Amount of byte read from the Read Pipe handle
Dim strBuff As String * 256 'String buffer reading the Pipe

'if the parameter is not empty update the CommandLine property
If Len(CommandLine) > 0 Then
mCommand = CommandLine
End If

'if the command line is empty then exit whit a error message
If Len(mCommand) = 0 Then
MsgBox "Command Line empty", vbCritical
Exit Function
End If

'Create the Pipe
sa.nLength = Len(sa)
sa.bInheritHandle = 1&
sa.lpSecurityDescriptor = 0&
ret = CreatePipe(hReadPipe, hWritePipe, sa, 0)

If ret = 0 Then
'If an error occur during the Pipe creation exit
MsgBox "CreatePipe failed. Error: " & Err.LastDllError, vbCritical
Exit Function
End If

'Launch the command line application
start.cb = Len(start)
start.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
'set the StdOutput and the StdError output to the same Write Pipe handle
start.hStdOutput = hWritePipe
start.hStdError = hWritePipe
'Execute the command
ret& = CreateProcessA(0&, mCommand, sa, sa, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

If ret <> 1 Then
'if the command is not found ....
MsgBox "File or command not found", vbCritical
Exit Function
End If

'Now We can ... must close the hWritePipe
ret = CloseHandle(hWritePipe)
mOutputs = ""

'Read the ReadPipe handle
Do
ret = ReadFile(hReadPipe, strBuff, 256, lngBytesread, 0&)
mOutputs = mOutputs & Left(strBuff, lngBytesread)
'Send data to the object via ReceiveOutputs event
RaiseEvent ReceiveOutputs(Left(strBuff, lngBytesread))
Loop While ret <> 0

'Close the opened handles
ret = CloseHandle(proc.hProcess)
ret = CloseHandle(proc.hThread)
ret = CloseHandle(hReadPipe)

'Return the Outputs property with the entire DOS output
ExecuteCommand = mOutputs
End Function
fzx4936 2009-08-06
  • 打赏
  • 举报
回复
1楼的代码基本上可以做为同步调用检查的通用模块了
chinaboyzyq 2009-08-06
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 derbywgz 的回复:]
如何启动定时器?

就是说我在执行完批处理后,启动定时器?
[/Quote]

定时器默认从程序启动时开始启动。
当然你可先关闭它,用tiemr1.enabled=false关闭,用timer1.enabled=ture打开。
神马都能聊 2009-08-06
  • 打赏
  • 举报
回复
用findwindow找dos窗口是否可用呢?
derbywgz 2009-08-06
  • 打赏
  • 举报
回复
如何启动定时器?

就是说我在执行完批处理后,启动定时器?
chinaboyzyq 2009-08-06
  • 打赏
  • 举报
回复
添加timer控件,设置它的设置它的Interval 为10000,这是10秒,1000为1秒

双击timer控件,然后添加你的判断程序。
derbywgz 2009-08-06
  • 打赏
  • 举报
回复
用定时器倒是不错
不过没有用过定时器,怎么用呢,请指教,谢谢!
chinaboyzyq 2009-08-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 samtyty 的回复:]
估计楼主是等第一个bat执行完后,从里面获取一些结果,然后执行第二个bat,我的程序就是要等执行完后,从输出的结果中提取一些参数,然后进行下一步计算
[/Quote]

可不可以用timer控件比如10秒一判断,如果有想要的结果则进行第二个BAT,没有就不执行,我觉得这样简单些。
chinaboyzyq 2009-08-06
  • 打赏
  • 举报
回复
不知道你批处理里面调用的是什么?

derbywgz 2009-08-06
  • 打赏
  • 举报
回复
是要等第一个批处理的结果

Public Const SYNCHRONIZE = &H100000
Public Const INFINITE = &HFFFFFFFF

这个定义在VB中怎么是红色的??
samtyty 2009-08-06
  • 打赏
  • 举报
回复
估计楼主是等第一个bat执行完后,从里面获取一些结果,然后执行第二个bat,我的程序就是要等执行完后,从输出的结果中提取一些参数,然后进行下一步计算
derbywgz 2009-08-06
  • 打赏
  • 举报
回复
及时将批处理合并了,还是要判断批处理执行的结果,这样

还是要等待批处理执行完成
chinaboyzyq 2009-08-06
  • 打赏
  • 举报
回复
你在第一个批处理文件的末尾加第二个bat不就行了,
干嘛用两次shell两个bat呢?

samtyty 2009-08-06
  • 打赏
  • 举报
回复

Public Const SYNCHRONIZE = &H100000
Public Const INFINITE = &HFFFFFFFF

Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long



Dim pId As Long, pHnd As Long ' 分别声明 Process Id 及 Process Handle 变数
Dim CVtemp As String, CVarray As Variant
pId = Shell(App.Path & "\" & sim, 0) ' Shell 传回 Process Id
pHnd = OpenProcess(SYNCHRONIZE, 0, pId) ' 取得 Process Handle
If pHnd <> 0 Then
Call WaitForSingleObject(pHnd, INFINITE) ' 无限等待,直到程序结束
Call CloseHandle(pHnd)
End If


这是我自己一个程序中的代码,你凑合着看看,自己修改一下吧,
关键是这一句吧:Call WaitForSingleObject(pHnd, INFINITE) ' 无限等待,直到程序结束
chinaboyzyq 2009-08-06
  • 打赏
  • 举报
回复
你在第一个批处理文件的末尾加第二个bat不就行,
干嘛用两次shell呢?
samtyty 2009-08-06
  • 打赏
  • 举报
回复

Public Const SYNCHRONIZE = &H100000
Public Const INFINITE = &HFFFFFFFF

Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long



Dim pId As Long, pHnd As Long ' 分别声明 Process Id 及 Process Handle 变数
Dim CVtemp As String, CVarray As Variant
pId = Shell(App.Path & "\" & sim, 0) ' Shell 传回 Process Id
pHnd = OpenProcess(SYNCHRONIZE, 0, pId) ' 取得 Process Handle
If pHnd <> 0 Then
Call WaitForSingleObject(pHnd, INFINITE) ' 无限等待,直到程序结束
Call CloseHandle(pHnd)
End If


这是我自己一个程序中的代码,你凑合着看看,自己修改一下吧,
关键是这一句吧:Call WaitForSingleObject(pHnd, INFINITE) ' 无限等待,直到程序结束

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧