When a client handle is created, it is associated with the current
thread that created it. Normally, if another thread attempts to
perform an operation using that handle, an error is returned since it
does not own the handle. This is used to ensure that other threads
cannot interfere with an operation being performed by the owner
thread. In some cases, it may be desirable for one thread in a client
application to create the client handle, and then pass that handle to
another worker thread. The AttachThread method can be used to
change the ownership of the handle to the new worker thread. By
preserving the return value from the method, the original owner of
the handle can be restored before the worker thread terminates.
This method should be called by the new thread immediately after
it has been created, and if the new thread does not release the
handle itself, the ownership of the handle should be restored to the
parent thread before it terminates. Under no circumstances should
AttachThread be used to forcibly release a handle allocated by
another thread while a blocking operation is in progress. To cancel
an operation, use the Cancel method and then release the
handle after the blocking method exits and control is returned to the
current thread.
Note that the dwThreadId parameter is presumed to be a
valid thread ID and no checks are performed to ensure that the thread
actually exists. Specifying an invalid thread ID will orphan the
client handle used by the class until the destructor is called.