Binarysharp.MemoryManagement.Threading.ThreadCore.TerminateThread C# (CSharp) Method

TerminateThread() public static method

Terminates a thread.
public static TerminateThread ( SafeMemoryHandle threadHandle, int exitCode ) : void
threadHandle Binarysharp.MemoryManagement.Native.SafeMemoryHandle A handle to the thread to be terminated.
exitCode int The exit code for the thread.
return void
        public static void TerminateThread(SafeMemoryHandle threadHandle, int exitCode)
        {
            // Check if the handle is valid
            HandleManipulator.ValidateAsArgument(threadHandle, "threadHandle");

            // Terminate the thread
            var ret = NativeMethods.TerminateThread(threadHandle, exitCode);

            // If the function failed
            if(!ret)
                throw new Win32Exception("Couldn't terminate the thread.");
        }

Usage Example

 /// <summary>
 ///     Terminates the thread.
 /// </summary>
 /// <param name="exitCode">The exit code of the thread to close.</param>
 public void Terminate(int exitCode = 0)
 {
     if (IsAlive)
     {
         ThreadCore.TerminateThread(Handle, exitCode);
     }
 }