# CmtWaitForThreadPoolFunctionCompletion

int CmtWaitForThreadPoolFunctionCompletion (int poolHandle, int threadFunctionID, unsigned int options);

## Purpose

Waits for the specified Thread Function to finish executing.

Instead of using this function to wait for a thread to complete, you can use CmtScheduleThreadPoolFunctionAdv to specify a callback function that is called when the Thread Function finishes executing.

Threads initialized with the multithreaded ActiveX threading style internally wait on ActiveX/COM locks to synchronize access to ActiveX/COM servers and objects. If the thread that calls CmtWaitForThreadPoolFunctionCompletion and the thread pool function thread both use ActiveX functions, a deadlock between the two threads could occur. Even if you call CmtWaitForThreadPoolFunctionCompletion with the OPT_TP_PROCESS_EVENTS_WHILE_WAITING option, multithreaded-style threads may deadlock.

Apartment-threaded style threads always own a window and use the message queue, instead of locks, to synchronize access to COM. Waiting on apartment-threaded style threads with the OPT_TP_PROCESS_EVENTS_WHILE_WAITING option tends to prevent deadlocks.

| Note Call CA_InitActiveXThreadStyleForCurrentThread CA_InitActiveXThreadStyleForCurrentThread to set the ActiveX threading style for a thread.

## Parameters

Input
Name | Type | Description
poolHandle | integer | The handle you obtained from CmtNewThreadPool to identify the thread pool. Pass DEFAULT_THREAD_POOL_HANDLE to specify the default thread pool.
threadFunctionID | integer | The threadFunctionID that you obtained from CmtScheduleThreadPoolFunction or CmtScheduleThreadPoolFunctionAdv.
options | unsigned integer | Pass OPT_TP_PROCESS_EVENTS_WHILE_WAITING if you want this function to process events while waiting for the Thread Function to finish executing. Pass 0 if you do not want this function to process events while waiting. Any thread that creates a panel or window and does not destroy it must process events while it is waiting. If the thread does not process events while it is waiting, the system might hang. To avoid hanging the system, pass OPT_TP_PROCESS_EVENTS_WHILE_WAITING if CmtWaitForThreadPoolFunctionCompletion might wait for more than a fraction of a second or if the thread you are waiting for sends messages or displays panels or dialog boxes.
| Note It is not always obvious when a window is created. Various LabWindows/CVI and Windows SDK functions create hidden windows. For example, the operating system uses hidden windows to communicate between certain kinds of ActiveX Automation servers and clients.

## Return Value

Name | Type | Description
CmtStatus | integer | The CmtStatus code that the function call returns. This function returns 0 to indicate success and negative values to indicate failure. Pass the CmtStatus code to CmtGetErrorMessage to get a description of the error code.