|
-
Jan 25th, 2005, 12:08 PM
#1
Thread Starter
Frenzied Member
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.
Being educated does not make you intelligent.
Need a weekend getaway??? Come Visit
-
Jan 25th, 2005, 12:21 PM
#2
Thread Starter
Frenzied Member
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.
Being educated does not make you intelligent.
Need a weekend getaway??? Come Visit
-
Jan 25th, 2005, 04:36 PM
#3
Thread Starter
Frenzied Member
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.
Being educated does not make you intelligent.
Need a weekend getaway??? Come Visit
-
Jan 25th, 2005, 06:46 PM
#4
Frenzied Member
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
-
Jan 25th, 2005, 07:30 PM
#5
Thread Starter
Frenzied Member
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.
Being educated does not make you intelligent.
Need a weekend getaway??? Come Visit
-
Jan 25th, 2005, 07:33 PM
#6
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
-
Jan 25th, 2005, 08:23 PM
#7
Frenzied Member
Re: Determine when batch file is done executing
Never tried it to be honest, never even looked at the Process class in depth.
-
Jan 26th, 2005, 09:10 AM
#8
Thread Starter
Frenzied Member
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
Last edited by Memnoch1207; Jan 26th, 2005 at 09:37 AM.
Being educated does not make you intelligent.
Need a weekend getaway??? Come Visit
-
Jan 26th, 2005, 10:17 AM
#9
Re: Determine when batch file is done executing
 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
-
Jan 26th, 2005, 11:10 AM
#10
Thread Starter
Frenzied Member
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
Being educated does not make you intelligent.
Need a weekend getaway??? Come Visit
-
Jan 27th, 2005, 07:43 AM
#11
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
-
Jan 27th, 2005, 09:26 AM
#12
Thread Starter
Frenzied Member
Re: Determine when batch file is done executing
 Originally Posted by ntg
seems to exit the loop if the process has finished execution or 8 seconds have passed...right ?
Exactly.
Being educated does not make you intelligent.
Need a weekend getaway??? Come Visit
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|