_cwait
等待,直到另一個處理序終止。
![]() |
---|
這個應用程式開發介面無法用來在 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 不是無效或是失敗的 GetExitCodeProcess 或 WaitForSingleObject API 的呼叫。 |
EINVAL |
action 無效。 |
如需這些與任何其他傳回碼的詳細資訊,請參閱 errno、 _doserrno、 _sys_errlist 和 _sys_nerr。
備註
_cwait 函式會等待指定之處理序的處理序 ID 的結束 procHandle提供。傳遞至 _cwait 的 procHandle 值應該是由 _spawn 函式的呼叫傳回建立指定之處理序的值。如果處理序 ID 結束,在呼叫 _cwait 之前, _cwait 會立即傳回。_cwait 可由所有流程用於有效的控制代碼的其他已知的處理序 (procHandle) 存在。
儲存指定的處理序傳回碼的緩衝區的termstat 。termstat 的值指出指定的處理序會呼叫 ExitProcess API 正常終止。如果指定的處理序呼叫 exit 或 _exit,從 main傳回或到達 main結尾,ExitProcess 會在內部呼叫。如需透過 termstat值的詳細資訊,請參閱 GetExitCodeProcess。使用 termstat的 null 值,如果 _cwait 呼叫,就不會儲存指定之處理序的傳回碼。
因為父 - 子關係 (Parent-Child Relationship)。這些環境,未實作 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