How to wait for a shelled application to finish by using Visual Basic 2005 or Visual Basic .NET (305368)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

This article was previously published under Q305368
For a Microsoft Visual C# .NET version of this article, see 305369.
For a Microsoft Visual C++ version of this article, see 307388.
For a Microsoft Visual Basic 6.0 version of this article, see 129796.

SUMMARY

This article demonstrates how to use the .NET Framework Process class to start another application from your code and have the code wait for the other application to close before it continues.

When the code waits for the application to finish, there are two options:
  • Wait indefinitely for the other application to either finish or be closed by the user.
  • Specify a time-out period after which you can close the application from your code.
This article presents two code samples that demonstrate both approaches. In addition, the time-out example allows the possibility that the other application may have stopped responding (hung) and takes the necessary steps to close the application.

Requirements

  • Microsoft Visual Basic 2005 or Microsoft Visual Basic .NET

Wait indefinitely for the shelled application to finish

The following code sample starts another application (in this case, Notepad) and waits indefinitely for the application to close.
    'How to Wait for a Shelled Process to Finish
    'Get the name of the system folder.
     Dim sysFolder As String = _
                 Environment.GetFolderPath(Environment.SpecialFolder.System)
    'Create a new ProcessStartInfo structure.
    Dim pInfo As New ProcessStartInfo()
    'Set the file name member of pinfo to Eula.txt in the system folder.
    pInfo.FileName = sysFolder & "\eula.txt"
    'Start the process.
    Dim p As Process = Process.Start(pInfo)
    'Wait for the process window to complete loading.
    p.WaitForInputIdle()
    'Wait for the process to exit.
    p.WaitForExit()
    'Continue with the code.
    MessageBox.Show("Code continuing...")
				

Provide a time-out for the shelled application

The following code sample sets a time-out for the shelled application. The time-out for this example is set to 5 seconds. You may want to adjust this number (which is calculated in milliseconds) for your testing.
    'Set a time-out value.
    Dim timeOut As Integer = 5000
    'Get the path to the system folder.
    Dim sysFolder As String = _
         Environment.GetFolderPath(Environment.SpecialFolder.System)
    'Create a new ProcessStartInfo structure.
    Dim pInfo As New ProcessStartInfo()
    'Set the file name member of pinfo to Eula.txt in the system folder.
    pInfo.FileName = sysFolder & "\eula.txt"
    'Start the process. 
    Dim p As Process = Process.Start(pInfo)
    'Wait for the process window to complete loading.
    p.WaitForInputIdle()
    'Wait for the process to exit.
    p.WaitForExit(timeOut)
    'HasExited is true if the application closed before the time-out.
    If p.HasExited = False Then
        'Process is still running.
        'Test to see if process is hung up.
        If p.Responding Then
            'Process was responding; close the main window.
            p.CloseMainWindow()
        Else
            'Process was not responding; force the process to close.
            p.Kill()
        End If
    End If
    MessageBox.Show("Code continuing...")
				

Troubleshooting

Sometimes it may be difficult to choose between these two options. The primary purpose of setting a time-out is to prevent your application from hanging because the other application has hung. Time-outs are better suited for a shelled application that performs background processing, in which the user may not know that the other application has stalled and does not have a convenient way to close it.

REFERENCES

For more information about the Process class, see the following .NET Framework Class Library documentation: For more information, click the following article number to view the article in the Microsoft Knowledge Base:

306394 How to determine when a shelled process ends in Visual Basic .NET or in Visual Basic 2005


Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005applies kbvs2005swept kbHOWTOmaster KB305368 kbAudDeveloper