Shell and Wait with Terminate, VB.Net Version - November 2002 - 2008
In December 1998 I wrote a 'Shell and Wait' tip. It has been the most downloaded tip from this site. This month I converted the code to use VB.Net. To view the VB6 version click here. Thanks to Tom Shelton for providing the basic code needed to start a 'process' and terminate it if desired. Typically the function is used to launch another application then wait for its completion. I attempted to maintain compatibility with the VB 6 version while converting the function to VB.Net. The call to the function has not been altered.
2008/07/01 - I converted the demo program for VS 2005. And I fixed one minor termination issue. See below for a link to the VS 2005 code.
2008/07/03 - I added the ability to pass arguments to the calling application. I rewrote the demo to load IE and link to the VB tips index page. This code is only available in the VS 2005 version.
The VB.Net Demo Program - Click the 'Launch' button to run the Internet Explorer and link to the tips index page. The demo waits for the termination of IE. End IE and the demo will resume operation. Force termination of IE by clicking the 'Terminate' button. Terminate can be disabled by checking the 'Disable Terminate' box. Entries are added to a log file if checked. View the log file by clicking the 'View the Log File' button. Delete the log file by clicking that 'Delete the Log File' button.
Module 'modShellAndWait.vb' contains the VB.Net code that provides support for the ShellAndWait and ShellAndWaitTerminate functions. The functions:
Function Description ShellAndWait Call this function to start the process. Information used by the function is passed in a user defined structure, see below. ShellAndWaitTerminate Call this function after calling ShellAndWait to terminate the started process. Pass the structure used by ShellAndWait..
Properties are passed to the functions in structure 'udtShellAndWait'. The properties are:
Property Type Description Sample sCommand String The command used by the shell operation. "IExplore.Exe" sArguments String The optional argument string passed to the calling application. Only available in the VS 2005 version after 2008/07/03. "http://www.larryrebich.com/tips" bShellAndWaitRunning Boolean This shell and wait operation is 'running'. Set in the function. bNoTerminate Boolean If true, no forced termination allowed [no DoEvents] and wait is indefinite. True/False lMilliseconds Long If termination is allowed, interrupt this often [DoEvents this often]. 1000 is one second. Default is one second. 2000/1000 bLogFile Boolean Write entries into the log file. See below. True/False sLogFile String Fully qualified name of the log file. See below. "c:\myfolder\mylogfile.txt" lMaxSize Long Maximum log file size before being deleted, default is 100K. 20 * 1024 '20K
100 * 1024 '100K
dStart Date Date/Time the process started. Set in the function. dFinish Date Date/Time the process finished. Set in the function. dDiff Date Date/Time difference. Set in the function. lSeconds Long Duration in seconds. Set in the function. Added for the VB.Net version. Not in the VB 6 version. bTerminated Boolean Set true if terminated by the ShellAndWaitTerminate function. dTerminated Date Date/Time if 'forced termination'. Set in the ShellAndWaitTerminate function. tProcess Process_Information Points to a 'System.Diagnostics.Process' object in structure Process_Information. Set in the function. tStart StartupInfo Not used in the VB.Net version. Retained for compatibility with the VB 6 version.
Sample Log File
Optionally the start, finish and duration, the shelled command and, if terminated, the termination time are captured in the file.
Using the Routines
Add the following module to your application if you want to use these routines:
Module Description modShellAndWait.vb The routines described in this document.
Used in the demo, not needed in your application:
Module - Click the link for details. Description modLocationLocationLocation.vb Used to add 'Window' support to the demo. modRegRead.vb Used by modLocationLocationLocation. BGWebConnect.vb Connect to the web using a URL and the System.Diagnostics.Process class. BGAbout.vb Provide an About message. BGInfo.vb Connect to the web and display this page. BGHelpMenu.vb Dynamically create the help menu. modPath.vb Contains functions used to get the application path. Includes an 'AppPath' function.
Download VB Code
Click here to download the VB.Net VS 2005 Source Code [32k]. Click here to download the VB.Net VS 2003 Source Code [29k]. Click here to download the VB 6 Source Code [34k]. Click here to view the VB 6 version's documentation.
If you download this tip please consider making a modest donation of a $1.00 or more.
Previous Tip Tip Index Next Tip First Tip