.NET Framework Developer Center > .NET Development Forums > Common Language Runtime > Using WaitForExit() causes my program to hang
Ask a questionAsk a question
 

AnswerUsing WaitForExit() causes my program to hang

  • Thursday, October 29, 2009 6:07 AMSiggy01 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Using WaitForExit() causes my program to "hang" at this statement.
    Without the WaitForExit() my program competes under 1 minute.
    Please check my code below and let me know what I am doing incorrectly.

    Dim m_oProc As New Process
    Dim oInfo As ProcessStartInfo

    oInfo = New ProcessStartInfo("some.wsf", sArgs)
    oInfo.UseShellExecute = False
    oInfo.RedirectStandardOutput = True
    oInfo.RedirectStandardError = True
    oInfo.CreateNoWindow = True
    m_oProc.StartInfo = oInfo

    Dim et As New StdErrorThread(m_oProc)
    Dim t As New Thread(AddressOf et.ThreadProc)
    et.ErrLogObj = ErrLogObj

    If m_oProc.Start() Then
       t.Start()
       m_oProc.WaitForExit()
    ...

    Without the WaitForExit the "some.wsf" file completes within a minute.
    With the WaitForExit the script does not complete and my code hangs.

    Why?

    If I use WaitForExit(30000) then execution continues on the next line only AFTER 5 minutes!

    Why?

     

Answers

  • Thursday, October 29, 2009 11:12 AMJialiang Ge [MSFT]MSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hello Siggy01

    In which thread are you calling m_oProc.WaitForExit()? Is it the WinForm UI thread? If yes, please move the call to a worker thread. It's not recommended to wait in UI thread for I/O or external applications that may take a long time.


    Regards,
    Jialiang Ge
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked As Answer bySiggy01 Friday, November 06, 2009 10:54 PM
    •  
  • Friday, November 06, 2009 10:53 PMSiggy01 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Jialiang,

    Thanks for your response and sorry about the delayed response from me.

    I have now created a separate thread for the stdout as well as the stderr. 
    So in effect I have the main thread plus a thread for each of the 2 results that I am receiving.
    The WaitForExit is being executed on the main thread and all is working as it should.

    Once again thanks for pointing me in the correct direction.

    Regards
    Siggy
    • Marked As Answer bySiggy01 Friday, November 06, 2009 10:54 PM
    •  

All Replies

  • Thursday, October 29, 2009 6:10 AMSiggy01 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    ... correction to my comment above, the "WaitForExit(30000)" should read "WaitForExit(300000)"!

  • Thursday, October 29, 2009 11:12 AMJialiang Ge [MSFT]MSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hello Siggy01

    In which thread are you calling m_oProc.WaitForExit()? Is it the WinForm UI thread? If yes, please move the call to a worker thread. It's not recommended to wait in UI thread for I/O or external applications that may take a long time.


    Regards,
    Jialiang Ge
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked As Answer bySiggy01 Friday, November 06, 2009 10:54 PM
    •  
  • Thursday, October 29, 2009 12:14 PMnobugzMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    You are redirecting output but there's no evidence that you are actually reading the output.  Doing it after the WaitForExit() call is usually too late.  The redirected app's output buffer will fill up and then it will block, waiting for somebody to empty the buffer.  Use BeginOutputReadLine() to avoid this.

    Hans Passant.
  • Thursday, November 05, 2009 10:47 AMJialiang Ge [MSFT]MSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello Siggy01

    How are you? Do you get any progress in the issue? If you have any other questions, please feel free to post here.
    Regards,
    Jialiang Ge
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Friday, November 06, 2009 10:53 PMSiggy01 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi Jialiang,

    Thanks for your response and sorry about the delayed response from me.

    I have now created a separate thread for the stdout as well as the stderr. 
    So in effect I have the main thread plus a thread for each of the 2 results that I am receiving.
    The WaitForExit is being executed on the main thread and all is working as it should.

    Once again thanks for pointing me in the correct direction.

    Regards
    Siggy
    • Marked As Answer bySiggy01 Friday, November 06, 2009 10:54 PM
    •