Determine when batch file is done executing
I am executing a batch file from within my application and I was curious if there was a way of determining when the execution had completed???
I know I could use the WaitForExit(value) method, but this batch file executes the DTSRun.exe application to import data from a text file into a SQL Server database, so its execution time may vary.
Re: Determine when batch file is done executing
Nevermind, I just created a Sub that executes the batch file, the when its finished it just returns to the Sub that called it.
Re: Determine when batch file is done executing
Actually, this didn't work...It appears the process is being run in another thread.
Does anyone know how I can determine when the process (batch file) is done executing and I can continue executing my code.
Re: Determine when batch file is done executing
Not sure how to tell when a batch file is done, but maybe you can achieve the same result in a different way.
Try dropping into the API using CreateProcess() or CreateProcessAsUser() and then call WaitForSingleObject() - use the INFINITE value in the last one. All this stuff is on MSDN, obviously.
Oh, not that I've tried it in .NET :) - I would imagine it would work just fine, though. Quick look at the .NET Process class, not sure if you can achieve the same results.
Mike
Re: Determine when batch file is done executing
It appears the Process classes HasExited property might be what I am looking for, thanks for the assistance.
Re: Determine when batch file is done executing
Actually, HasExited has worked for me in the past for batch files. I'm not sure whether that would work if the batch file issued a "start" command. Doesn't HasExited work for you ?
Cheers,
NTG
Re: Determine when batch file is done executing
Never tried it to be honest, never even looked at the Process class in depth.
Re: Determine when batch file is done executing
I did it like this
In my import module
VB Code:
Public Sub DoStuff()
If Not mobjDTSProcess.HasExited OrElse mintI = 4 Then
Thread.Sleep(2000)
NotifyUser("Waiting for DTS process to complete.")
mintI += 1
End
End Sub
Function within my main module
VB Code:
Public Sub ExecuteBatchFile(ByVal strFilePath As String)
Try
'Execute the batch file
mobjDTSProcess = Process.Start(strFilePath)
Catch ex As Exception
'Catch the exception
End Try
End Sub
Re: Determine when batch file is done executing
Quote:
Originally Posted by Memnoch1207
VB Code:
Public Sub DoStuff()
If Not mobjDTSProcess.HasExited OrElse mintI = 4 Then
Thread.Sleep(2000)
NotifyUser("Waiting for DTS process to complete.")
mintI += 1
End
End Sub
So how do you call DoStuff()? If you call it once, it's going to either enter the If statement one time or none at all and then exit but it's not going to wait for the process to quit. I was thinking of something along the lines of:
VB Code:
While mobjDTSProcess.HasExited = False
Thread.Sleep(50)
End While
Cheers,
NTG
Re: Determine when batch file is done executing
My mistake, it should have been a Do...Loop, not an If..Then
I have 1 main (global) module, that contains Sub Main, then 5 individual import modules that execute my DTS packages to import 5 different text files.
The DoStuff module is in on of my import modules and is called from the main module.
The ExecuteBatchFile procedure is contained in the main module, so the processes.HasExited property can be accessed by the import modules. Because each import module calls a different batch to execute the DTSRun.exe command to run a DTS package.
It basically looks like this (extremely scaled down)
Main Module
VB Code:
Public mobjDTSProcess As Process
Public Sub Main()
Call DoStuff()
End Sub
Public Sub ExecuteBatchFile(ByVal strFilePath As String)
Try
mobjDTSProcess = Process.Start(strFilePath)
Catch ex As Exception
'Do something
End Try
End Sub
Import1 Module
VB Code:
Public mintI As Integer
Public Sub DoStuff()
ExecuteBatchFile("C:\Test.bat")
Do until mobjDTSProcess.HasExited = True OrElse mintI = 4
NotifyUser("Waiting on DTS to complete.")
Thread.Sleep(2000)
mintI += 1
Loop
If mintI = 4 Then
'The DTS process failed
Application.Exit()
Else
'The DTS process succeeded
'Do more stuff
End If
End Sub
Re: Determine when batch file is done executing
The code:
VB Code:
Do until mobjDTSProcess.HasExited = True OrElse mintI = 4
NotifyUser("Waiting on DTS to complete.")
Thread.Sleep(2000)
mintI += 1
Loop
seems to exit the loop if the process has finished execution or 8 seconds have passed...right ?
Cheers,
NTG
Re: Determine when batch file is done executing
Quote:
Originally Posted by ntg
seems to exit the loop if the process has finished execution or 8 seconds have passed...right ?
Exactly.