共用方式為


_cwait

等到其他處理序終止為止。

重要

這個 API 不能用於在 Windows 執行階段中執行的應用程式。 如需詳細資訊,請參閱 CRT functions not supported in Universal Windows Platform apps (通用 Windows 平台應用程式中不支援的 CRT 函式)。

語法

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

參數

termstat
緩衝區的指標,其中將儲存指定進程的結果碼,或 NULL

procHandle
要等待之處理序的處理常式 (也就是說,該處理序必須先終止,才能傳回 _cwait)。

action
NULL:Windows 作業系統應用程式忽略;適用于其他應用程式:在 上 procHandle 執行的動作程式碼。

傳回值

當指定處理序成功完成時,會傳回指定處理序的處理常式,並將 termstat 設定為指定處理序傳回的結果碼。 否則,會傳回 -1 並如下所示設定 errno

errno 描述
ECHILD 沒有指定的進程存在、 procHandle 無效,或呼叫 GetExitCodeProcessWaitForSingleObject API 失敗。
EINVAL action 無效。

如需這些傳回碼和其他傳回碼的詳細資訊,請參閱 errno_sys_errlist_doserrno_sys_nerr

備註

_cwait 函式會等待 procHandle 所提供之指定處理序的處理序識別碼終止。 procHandle傳遞至 _cwait 的值應該是呼叫 _spawn 建立指定進程之函式所傳回的值。 若處理序識別碼在呼叫 _cwait 之前終止,則會立即傳回 _cwait_cwait 可供所有處理序用於等待其他所有已知處理序 (其中有有效的處理常式 procHandle)。

termstat 指出要儲存指定處理序的傳回碼的緩衝區。 的值 termstat 表示指定的進程是否透過呼叫 Windows ExitProcess API 正常終止。 若指定處理序呼叫 ExitProcessexit (從 _exit 傳回,或達到 main 的結尾) 時,會從內部呼叫 main。 如需傳回 termstat 之值的詳細資訊,請參閱 GetExitCodeProcess 。 如果使用 _cwait 的值 termstat 呼叫 NULL ,則不會儲存指定進程的傳回碼。

actionWindows 作業系統會忽略 參數,因為這些環境中不會實作父子式關聯性。

除非 procHandle 為 -1 或 -2 (目前處理序或執行緒的處理常式),否則會關閉處理常式。 在此情況下,請勿使用傳回的控制碼。

根據預設,此函式的全域狀態會限定于應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。

需求

常式 必要的標頭 選擇性標頭
_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
{
    intptr_t hProcess;
    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].hProcess = _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].hProcess, _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]);
    }
}

輸出的順序會因執行而異。

Hi, Dad. It's Ann.
Come here, Ann.
Thank you, Ann.
Hi, Dad. It's Beth.
Hi, Dad. It's Carl.
Hi, Dad. It's Dave.

另請參閱

進程和環境控制
_spawn_wspawn 函式