Results 1 to 12 of 12

Thread: Determine when batch file is done executing

  1. #1

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861

    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

  2. #2

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861

    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

  3. #3

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861

    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

  4. #4
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690

    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

  5. #5

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861

    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

  6. #6
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449

    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
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  7. #7
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690

    Re: Determine when batch file is done executing

    Never tried it to be honest, never even looked at the Process class in depth.

  8. #8

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861

    Re: Determine when batch file is done executing

    I did it like this

    In my import module
    VB Code:
    1. Public Sub DoStuff()
    2.    If Not mobjDTSProcess.HasExited OrElse mintI = 4 Then
    3.       Thread.Sleep(2000)
    4.       NotifyUser("Waiting for DTS process to complete.")
    5.       mintI += 1
    6.    End
    7. End Sub

    Function within my main module
    VB Code:
    1. Public Sub ExecuteBatchFile(ByVal strFilePath As String)
    2.    Try
    3.       'Execute the batch file
    4.       mobjDTSProcess = Process.Start(strFilePath)
    5.    Catch ex As Exception
    6.       'Catch the exception
    7.    End Try
    8. 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

  9. #9
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449

    Re: Determine when batch file is done executing

    Quote Originally Posted by Memnoch1207
    VB Code:
    1. Public Sub DoStuff()
    2.    If Not mobjDTSProcess.HasExited OrElse mintI = 4 Then
    3.       Thread.Sleep(2000)
    4.       NotifyUser("Waiting for DTS process to complete.")
    5.       mintI += 1
    6.    End
    7. 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:
    1. While mobjDTSProcess.HasExited = False
    2.    Thread.Sleep(50)
    3. End While

    Cheers,
    NTG
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  10. #10

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861

    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:
    1. Public mobjDTSProcess As Process
    2.  
    3. Public Sub Main()
    4.    Call DoStuff()
    5. End Sub
    6.  
    7. Public Sub ExecuteBatchFile(ByVal strFilePath As String)
    8.    Try
    9.       mobjDTSProcess = Process.Start(strFilePath)
    10.    Catch ex As Exception
    11.       'Do something
    12.    End Try
    13. End Sub

    Import1 Module
    VB Code:
    1. Public mintI As Integer
    2.  
    3. Public Sub DoStuff()
    4.    ExecuteBatchFile("C:\Test.bat")
    5.  
    6.    Do until mobjDTSProcess.HasExited = True OrElse mintI = 4
    7.       NotifyUser("Waiting on DTS to complete.")
    8.       Thread.Sleep(2000)
    9.       mintI += 1
    10.    Loop
    11.  
    12.    If mintI = 4 Then
    13.       'The DTS process failed
    14.       Application.Exit()
    15.    Else
    16.       'The DTS process succeeded
    17.       'Do more stuff
    18.    End If
    19. End Sub
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  11. #11
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449

    Re: Determine when batch file is done executing

    The code:
    VB Code:
    1. Do until mobjDTSProcess.HasExited = True OrElse mintI = 4
    2.       NotifyUser("Waiting on DTS to complete.")
    3.       Thread.Sleep(2000)
    4.       mintI += 1
    5.    Loop

    seems to exit the loop if the process has finished execution or 8 seconds have passed...right ?

    Cheers,
    NTG
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  12. #12

    Thread Starter
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861

    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.
    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
  •  



Click Here to Expand Forum to Full Width