User Tools

Site Tools


waiting_20for_20an_20external_20program_20to_20terminate

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
waiting_20for_20an_20external_20program_20to_20terminate [2018/03/31 13:19] – external edit 127.0.0.1waiting_20for_20an_20external_20program_20to_20terminate [2024/01/05 00:21] (current) – external edit 127.0.0.1
Line 1: Line 1:
 =====Waiting for an external program to terminate===== =====Waiting for an external program to terminate=====
  
-//by Richard Russell, June 2006//\\ \\  The ***RUN** command (or, equivalently, the **OSCLI "RUN"** statement) executes a specified external application and (by default) //waits for the application to terminate// before returning control to your BASIC program. This is often what is required: for example if the external application creates a file you probably don't want to continue execution of your BASIC program until the file has been created.\\ \\  This is fine when ***RUN** does what you need, but on occasion you may need to use the additional facilities provided by the **ShellExecute** Windows API function. In particular **ShellExecute** allows you to specify the working directory used by the external application and to specify the //show state// of that application's window. For example it allows you to run the application in a //hidden// window, which may well be useful when you don't want the user to be troubled by the appearance of another window.\\ \\  The syntax of the **ShellExecute** function when used for this purpose is as follows:\\ +//by Richard Russell, June 2006//\\ \\  The ***RUN** command (or, equivalently, the **OSCLI "RUN"** statement) executes a specified external application and (by default) //waits for the application to terminate// before returning control to your BASIC program. This is often what is required: for example if the external application creates a file you probably don't want to continue execution of your BASIC program until the file has been created.\\ \\  This is fine when ***RUN** does what you need, but on occasion you may need to use the additional facilities provided by the **ShellExecute** Windows API function. In particular **ShellExecute** allows you to specify the working directory used by the external application and to specify the //show state// of that application's window. For example it allows you to run the application in a //hidden// window, which may well be useful when you don't want the user to be troubled by the appearance of another window.\\ \\  The syntax of the **ShellExecute** function when used for this purpose is as follows: 
 + 
 +<code bb4w> 
         SYS "ShellExecute", @hwnd%, 0, prog$, parm$, cdir$, show%         SYS "ShellExecute", @hwnd%, 0, prog$, parm$, cdir$, show%
-Here **prog$** is the name of the application you want to run (e.g. ""NOTEPAD.EXE""), **parm$** contains the //command line// parameters for the application, if any (in the case of an editor like NOTEPAD that would typically be the name of the file to edit), **cdir$** is the working directory you want the application to use and **show%** is the window's show state (e.g. 0 to hide the window, **1** to show it normally).\\ \\  So you might for example use a statement such as this:\\ +</code> 
 + 
 +Here **prog$** is the name of the application you want to run (e.g. ""NOTEPAD.EXE""), **parm$** contains the //command line// parameters for the application, if any (in the case of an editor like NOTEPAD that would typically be the name of the file to edit), **cdir$** is the working directory you want the application to use and **show%** is the window's show state (e.g. 0 to hide the window, **1** to show it normally).\\ \\  So you might for example use a statement such as this: 
 + 
 +<code bb4w>
         SYS "ShellExecute", @hwnd%, 0, "notepad.exe", "test.txt", "C:\", 1         SYS "ShellExecute", @hwnd%, 0, "notepad.exe", "test.txt", "C:\", 1
-However the use of **ShellExecute** has one significant disadvantage compared to the use of ***RUN**: it does //not// wait until the external application has terminated. Control will be returned to your BASIC program immediately.\\ \\  To overcome this disadvantage you can use the procedure listed below. This behaves like **ShellExecute** except that it does not return until the external application has terminated:\\ +</code> 
 + 
 +However the use of **ShellExecute** has one significant disadvantage compared to the use of ***RUN**: it does //not// wait until the external application has terminated. Control will be returned to your BASIC program immediately.\\ \\  To overcome this disadvantage you can use the procedure listed below. This behaves like **ShellExecute** except that it does not return until the external application has terminated: 
 + 
 +<code bb4w> 
         DEF PROCexecuteandwait(prog$,parm$,cdir$,show%)         DEF PROCexecuteandwait(prog$,parm$,cdir$,show%)
         LOCAL sei{}, res%         LOCAL sei{}, res%
Line 27: Line 37:
         SYS "CloseHandle", sei.hProcess%         SYS "CloseHandle", sei.hProcess%
         ENDPROC         ENDPROC
-So, by analogy with the example above, you might call this procedure as follows:\\ +</code> 
 + 
 +So, by analogy with the example above, you might call this procedure as follows: 
 + 
 +<code bb4w>
         PROCexecuteandwait("notepad.exe", "test.txt", "C:\", 1)         PROCexecuteandwait("notepad.exe", "test.txt", "C:\", 1)
-If you need to know the exit code (ERRORLEVEL) of the program then the above code can be converted to a function which returns this value:\\ +</code> 
 + 
 +If you need to know the exit code (ERRORLEVEL) of the program then the above code can be converted to a function which returns this value: 
 + 
 +<code bb4w>
         DEF FNexecuteandwait(prog$,parm$,cdir$,show%)         DEF FNexecuteandwait(prog$,parm$,cdir$,show%)
         LOCAL sei{}, res%         LOCAL sei{}, res%
Line 52: Line 70:
         SYS "CloseHandle", sei.hProcess%         SYS "CloseHandle", sei.hProcess%
         = res%         = res%
-You might call it as follows:\\ +</code> 
 + 
 +You might call it as follows: 
 + 
 +<code bb4w>
         exitcode% = FNexecuteandwait("notepad.exe", "test.txt", "C:\", 1)         exitcode% = FNexecuteandwait("notepad.exe", "test.txt", "C:\", 1)
 +</code>
waiting_20for_20an_20external_20program_20to_20terminate.1522502391.txt.gz · Last modified: 2024/01/05 00:16 (external edit)