Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to find out a particular task is completed or not. 1

Status
Not open for further replies.

indiaa123

Programmer
Aug 27, 2003
25
US
I am trying to execute three batch files through VB application as given below

Task1 = Shell("\\..\..\" & "softwaresetup.bat", vbHide)
Task2 = Shell("\\..\..\" & "packagesetup.bat", vbHide)
Task3 = Shell("\\..\.." & "serviceupdate.bat", vbHide)

Task1 takes about say x minutes minutes, Task2 takes Y minutes and Task3 takes Z min. Problem I have is, Task2 gets started even before completing the Task1 and I don't want that as both tasks display some common screens and it makes difficult to understand which screen belongs to which task.

How can i know whether task1 has completed succesfully?

Is there anyway I can control the execution of Task2 and Task3. I want these tasks to execute only when the prior task is completed?

 
You can pause your app's thread until the shelled batch file closes. This sample function will also return the batch file's exit code.
Code:
Public Type STARTUPINFO
 cb As Long
 lpReserved As String
 lpDesktop As String
 lpTitle As String
 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

Public Type PROCESS_INFORMATION
 hProcess As Long
 hThread As Long
 dwProcessID As Long
 dwThreadID As Long
End Type

Public Const NORMAL_PRIORITY_CLASS = &H20&
Public Const INFINITE = -1&

Public Declare Function WaitForSingleObject Lib "kernel32" (Byval _
hHandle As Long, Byval dwMilliseconds As Long) As Long

Public Declare Function CreateProcessA Lib "kernel32" (Byval _
lpApplicationName As Long, Byval lpCommandLine As String, Byval _
lpProcessAttributes As Long, Byval lpThreadAttributes As Long, _
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

Public Declare Function GetExitCodeProcess Lib "kernel32" _
(Byval hProcess As Long, lpExitCode As Long) As Long
'________________________________ 

Public Function fShellWait(strCmdLine As String) As Long

  '--- Shells the passed command line and waits for the process to finish
  '--- Returns the exit code of the shelled process

  Dim udtProc As PROCESS_INFORMATION
  Dim udtStart As STARTUPINFO
  Dim lngRtn As Long

  'Initialize the STARTUPINFO structure
  udtStart.cb = Len(udtStart)

  'Launch the shelled application and yield to it
   lngRtn = CreateProcessA(Clng(0), strCmdLine, Clng(0), Clng(0), Clng(1), _
   NORMAL_PRIORITY_CLASS, Clng(0), Clng(0), udtStart, udtProc)
   DoEvents

  'Wait for the shelled application to finish
  lngRtn = WaitForSingleObject(udtProc.hProcess, INFINITE)
  Call GetExitCodeProcess(udtProc.hProcess, lngRtn)
  Call CloseHandle(udtProc.hThread)
  Call CloseHandle(udtProc.hProcess)
  fShellWait = lngRtn

End Function

Call fShellWait to run each batch file and check the return code between calls.

Paul Bent
Northwind IT Systems
 
I copied the code in VB editor,
But i see some lines in red. Probably some piece of info is missing in this line

Public Const NORMAL_PRIORITY_CLASS = &H20&
Public Const INFINITE = -1&

Can you help me out.
 
I am getting a compile error in the following line.

Call CloseHandle(udtProc.hThread)
Call CloseHandle(udtProc.hProcess)


This function is not present i believe.
 
Oops, sorry about that; I didn't copy properly:

Declare Function CloseHandle Lib "kernel32.dll" _
(ByVal hObject As Long) As Long

Paul Bent
Northwind IT Systems
 
Thanks !

I have another problem now. This is lauching the MSDos prompt but not executing my .exe file (present in batch file) until I close the dos prompt.

Content of my batch file is:
C:\Myapp\setup.exe

As soon as I close the dos prompt, it starts executing the .exe file. I don't want to have the manual intervention during this process. Please help me what I can do to get rid of this.

Thanks
 
If the batch file is only running an exe then don't bother with the batch file. Pass "C:\Myapp\setup.exe" directly into the fShellWait Function.

Paul Bent
Northwind IT Systems
 
Thanks Paul

One more Question.

If the batch file is not running a exe but doing some other operation. How to handle this situation.
 
Under W9x/ME you can right click the batch file, select properties and select close on exit. Couldn't find this option on an XP Pro property sheet.

If the batch file is doing something like copying or deleting files you can dispense with the batch file and shell command.com with command line parameters. Use the /c switch to close the command window on completion.

Paul Bent
Northwind IT Systems
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top