locked
Running and Disposing of Process RRS feed

  • Question

  • User704587827 posted

    Hello, i need some help writing code which will run a process and then dispose of it completely without causing any memory leaks.  I would also like to apply a timeout to the process and terminate it if it does not complete within the time period.  Here is what I have:

     

    1    Dim pStart As New System.Diagnostics.Process
    2    try
    3                Dim startInfo As System.Diagnostics.ProcessStartInfo
    4                Dim Arguments As String
    5                 Arguments = ""' delcare my arguments here
    6     
    7                startInfo = New System.Diagnostics.ProcessStartInfo("C:\Program Files\Program\Program.exe", Arguments)
    8                pStart.StartInfo = startInfo
    9                pStart.Start()
    10   
    11               While pStart.HasExited = False
    12                   If pStart.StartTime.AddSeconds(120) < Now Then
    13                       pStart.Kill()
    14                       objStreamWriter = File.AppendText(ErrorFILENAME)
    15                       'write to log
    16                       objStreamWriter.Close()
    17                   End If
    18               End While
    19               pStart.Close()
    20   
    21   catch
    22     'handle error
    23   finally
    24        pStart.Dispose
    25   end try
    
    Are there any issues with the way I did this? 

    Thanks Very Much!

     

     

     

    Thursday, April 3, 2008 1:43 AM

Answers

  • User242249085 posted

    That's very funny looking C# (there is a VB forum here).

     

    You while loop is busy polling, which is not very friendly... and could stop your process completing in time because the loop is taking up all the CPU time to determine iof it has finished. Better off using Process.WaitForExit (which can take a timeout).

    Process.Kill is pretty drastic, depending on the nature of the target process, Process.CloseMainWindow might be a better option.

    (In the end, how to manage a child process is all dependent on requirements, so hard to provide anything specific.)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 3, 2008 4:52 AM
  • User242249085 posted

    The process is a cmd line application.  Would Process.CloseMainWindow still work since there is no GUI?
     

    In that case no.

    Start.WaitForExit(120000).  Will this close the process for me or will it continue with my code after the timeout?
     

    WaitForExit will not skill the process. But the return should tell you why it returned (process exited of  its own accord or timeout reached.)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 4, 2008 6:11 AM

All replies

  • User242249085 posted

    That's very funny looking C# (there is a VB forum here).

     

    You while loop is busy polling, which is not very friendly... and could stop your process completing in time because the loop is taking up all the CPU time to determine iof it has finished. Better off using Process.WaitForExit (which can take a timeout).

    Process.Kill is pretty drastic, depending on the nature of the target process, Process.CloseMainWindow might be a better option.

    (In the end, how to manage a child process is all dependent on requirements, so hard to provide anything specific.)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 3, 2008 4:52 AM
  • User704587827 posted

    Hello, thanks for the reply. The process is a cmd line application.  Would Process.CloseMainWindow still work since there is no GUI?

     

    Also, i would like to use pStart.WaitForExit(120000).  Will this close the process for me or will it continue with my code after the timeout?  Would this be a better solution:

    pStart.Start()

    pStart.WaitForExit(120000)

    pStart.Close()

    pStart.Dispose()

     

    Thanks!

    Thursday, April 3, 2008 7:29 PM
  • User242249085 posted

    The process is a cmd line application.  Would Process.CloseMainWindow still work since there is no GUI?
     

    In that case no.

    Start.WaitForExit(120000).  Will this close the process for me or will it continue with my code after the timeout?
     

    WaitForExit will not skill the process. But the return should tell you why it returned (process exited of  its own accord or timeout reached.)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 4, 2008 6:11 AM