private bool JoinCore(int millisecondsTimeout) { SafeWaitHandle waitHandle = _osHandle; int result; waitHandle.DangerousAddRef(); try { result = WaitHandle.WaitForSingleObject(waitHandle.DangerousGetHandle(), millisecondsTimeout); } finally { waitHandle.DangerousRelease(); } return(result == (int)Interop.Constants.WaitObject0); }
private bool JoinInternal(int millisecondsTimeout) { // This method assumes the thread has been started Debug.Assert(!GetThreadStateBit(ThreadState.Unstarted) || (millisecondsTimeout == 0)); SafeWaitHandle waitHandle = _osHandle; // If an OS thread is terminated and its Thread object is resurrected, _osHandle may be finalized and closed if (waitHandle.IsClosed) { return(true); } // Handle race condition with the finalizer try { waitHandle.DangerousAddRef(); } catch (ObjectDisposedException) { return(true); } try { int result; if (millisecondsTimeout == 0) { result = (int)Interop.mincore.WaitForSingleObject(waitHandle.DangerousGetHandle(), 0); } else { result = WaitHandle.WaitForSingleObject(waitHandle.DangerousGetHandle(), millisecondsTimeout, true); } return(result == (int)Interop.Constants.WaitObject0); } finally { waitHandle.DangerousRelease(); } }