/// <summary> /// Joins this thread with the callee's thread. /// </summary> public void Join() { if (!IsAlive) { throw new InvalidOperationException("Thread instance is not running."); } IsAlive = false; var resultCode = ThreadingPrimitives.JoinThread(id); switch (resultCode) { case ThreadResultCode.Success: break; case ThreadResultCode.Deadlock: throw new InvalidOperationException("Deadlock was detected while joining threads."); case ThreadResultCode.InvalidOperation: throw new InvalidOperationException("Cannot join threads."); case ThreadResultCode.ThreadNotFound: throw new InvalidOperationException("Unknown thread."); default: throw new Exception("An error occurred while trying to join threads."); } }
/// <summary> /// Starts executing the thread. /// </summary> public void Start() { if (HasStarted) { throw new InvalidOperationException("Thread instance has already been started."); } if (ThreadingPrimitives.CreateThread(entryPoint.Invoke, out id) != ThreadResultCode.Success) { throw new Exception("An error occurred while trying to start a new thread."); } entryPoint = null; IsAlive = true; }