_cwait

等待,直到另一个进程停止。

重要

此 API 不能在 Windows 运行时执行的应用程序。有关更多信息,请参见 CRT 函数不支持与 /ZW

intptr_t _cwait( 
   int *termstat,
   intptr_t procHandle,
   int action 
);

参数

  • termstat
    将存储到指定的结果代码处理缓冲区的指针则为 null。

  • procHandle
    为等待的进程的句柄 (即必须停止的进程,在 _cwait 可以返回之前)。

  • action
    空:忽略由 windows 操作系统应用程序;对于其他应用程序:要执行的操作代码在 procHandle。

返回值

当指定的进程已成功完成,返回指定的处理,并且设置 termstat 到由指定的返回处理结果代码。 否则,返回– 1 并如下所示设置 errno。

描述

ECHILD

未指定的进程已存在,procHandle 无效或对失败的 GetExitCodeProcessWaitForSingleObject API 的调用。

EINVAL

action 无效。

有关这些属性和其他的更多信息返回代码示例,请参见 errno、_doserrno、_sys_errlist和_sys_nerr

备注

_cwait 函数等待指定的进程 ID 的终止 procHandle提供处理。 传递给 _cwaitprocHandle 的值应为通过对 _spawn 函数的调用返回创建指定的值的过程。 如果进程 ID 停止,在 _cwait 调用之前,_cwait 立即返回。 _cwait 可由任何使用过程等待已知的任何其他为其处理有效句柄 (procHandle) 存在。

termstat 指向指定的返回代码处理要存储的缓冲区。 termstat 的值指示指定的是否处理正常终止通过调用 ExitProcess API。 因此,如果指定的进程调用 exit 或 _exit,从 main返回或到达 main,结束ExitProcess 在内部调用。 有关通过 termstat值的更多信息,请参见 GetExitCodeProcess。 使用 termstat的,null 值如果 _cwait 调用,指定的返回代码不会存储。

因为父子关系在这些环境中,未实现 action 参数由 windows 操作系统忽略。

除非 procHandle 为 -1 或 -2 (到当前的处理进程或线程),处理已关闭。 因此,在这种情况下,不要使用返回的句柄。

要求

实例

必需的标头

选项标头

_cwait

<process.h>

<errno.h>

有关更多兼容性信息,请参见 兼容性

示例

// crt_cwait.c
// compile with: /c
// This program launches several processes and waits
// for a specified process to finish.
//
#define _CRT_RAND_S

#include <windows.h>
#include <process.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>


// Macro to get a random integer within a specified range
#define getrandom( min, max ) (( (rand_s (&number), number) % (int)((( max ) + 1 ) - ( min ))) + ( min ))

struct PROCESS
{
   int     nPid;
   char    name[40];
} process[4] = { { 0, "Ann" }, { 0, "Beth" }, { 0, "Carl" }, { 0, "Dave" } };

int main( int argc, char *argv[] )
{
   int termstat, c;
   unsigned int number;

   srand( (unsigned)time( NULL ) );    // Seed randomizer

   // If no arguments, this is the calling process
   if( argc == 1 )
   {
      // Spawn processes in numeric order
      for( c = 0; c < 4; c++ ){
         _flushall();
         process[c].nPid = _spawnl( _P_NOWAIT, argv[0], argv[0], 
                             process[c].name, NULL );
      }

      // Wait for randomly specified process, and respond when done 
      c = getrandom( 0, 3 );
      printf( "Come here, %s.\n", process[c].name );
      _cwait( &termstat, process[c].nPid, _WAIT_CHILD );
      printf( "Thank you, %s.\n", process[c].name );

   }
   // If there are arguments, this must be a spawned process 
   else
   {
      // Delay for a period that's determined by process number
      Sleep( (argv[1][0] - 'A' + 1) * 1000L );
      printf( "Hi, Dad. It's %s.\n", argv[1] );
   }
}
                     

.NET Framework 等效项

System::Diagnostics::Process::WaitForExit

请参见

参考

处理和环境控件

_spawn, _wspawn功能