SAMPLE: TERMWAIT Spawns Task and Waits for its Termination

Last reviewed: February 15, 1996
Article ID: Q84456
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) for Windows versions 3.0 and 3.1

SUMMARY

TERMWAIT is a file in the Microsoft Software Library that demonstrates how an application can launch a child task and then wait for it to complete before executing specific code. TERMWAIT uses notification messages from the Toolhelp dynamic-link library (DLL) to determine that the child task has completed. The techniques demonstrated by the TERMWAIT sample work for both Windows and MS-DOS (non-Windows) child tasks.

Download TERMWAIT.EXE, a self-extracting file, from the Microsoft Software Library (MSL) on the following services:

  • Microsoft Download Service (MSDL)

          Dial (206) 936-6735 to connect to MSDL
          Download TERMWAIT.EXE (size: 25487 bytes) 
    
  • Internet (anonymous FTP)

          ftp ftp.microsoft.com
          Change to the \SOFTLIB\MSLFILES directory
          Get TERMWAIT.EXE (size: 25487 bytes) 
    

MORE INFORMATION

The TERMWAIT sample application calls the NotifyRegister() function to install a notification callback function before it calls the WinExec() function to launch the child task. If a callback function is registered, it is called before any task in the system terminates. The notification callback function calls the TaskFindHandle() function to fill a TASKENTRY data structure with information about the terminating task. When the callback determines that the child task has terminated, it notifies the TERMWAIT application.

When it spawns the child task, TERMWAIT sets its bChildIsExecuting global variable to TRUE. The notification callback resets this variable to FALSE when the child task is complete. Any code that must not execute while the child task is running can query the value of the bChildIsExecuting flag. During the wait, any menu selections that will cause reentrancy problems should be disabled. Doing so keeps the user informed about the options that are presently valid. In the TERMWAIT sample, the AfterChildHasTerminated() function contains code that is executed only after the child task has completed.

If an application tracks a number of child tasks, its NotifyRegister() callback function should process both the NFY_STARTTASK and NFY_EXITTASK notifications. The callback function uses these notifications to maintain a list of child task handles. Note that while no two tasks will have the same handle, it is possible for task handles to be reused. Consequently, if one task ends and a new task begins, the new task can use the same task handle.

Version 3.1 of the Windows SDK is required to build the TERMWAIT sample. However, the compiled code is compatible with both Windows 3.0 and 3.1. Note that because the TOOLHELP.DLL is not part of the default installation for Windows 3.0, it must be installed into the Windows system directory (by default, C:\WINDOWS\SYSTEM) before TERMWAIT will run. The TOOLHELP.DLL file is provided as a redistributable file with the Windows 3.1 SDK.

NOTE: The NotifyRegister callback function must be placed in a separate source file and this file must be compiled with /GA/GEa. This is because the NotifyRegisterCallback function is called with the Data Segment (DS) of the task that installed the callback and the Stack egment of the task which is notifying the callback. So SS != DS and /GA/GEs cannot be used to compile this file.


Additional reference words: 3.00 3.10 synchronize softlib TERMWAIT.EXE
KBCategory: kbprg kbfile
KBSubcategory: KrToolHlp


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 15, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.