/// <summary> /// Reset the name of the set thread. /// </summary> /// <param name="thread" type="Thread">The thread.</param> /// <exception cref="System.NullReferenceException">Thread cannot be null</exception> static private void ResetThreadName(Thread thread) { if (null == thread) { throw new System.NullReferenceException("Thread cannot be null"); } lock (thread) { // // This is a private member of Thread, if they ever change the name this will not work // var field = thread.GetType().GetField("m_Name", BindingFlags.Instance | BindingFlags.NonPublic); if (null != field) { // // Change the Name to null (nothing) // field.SetValue(thread, null); // // This 'extra' null set notifies Visual Studio about the change // thread.Name = null; } } }
private static void ThreadAbort(Thread thread) { MethodInfo abort = null; foreach(MethodInfo m in thread.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)) { if (m.Name.Equals("AbortInternal") && m.GetParameters().Length == 0) abort = m; } if (abort == null) { throw new Exception("Failed to get Thread.Abort method"); } abort.Invoke(thread, new object[0]); }
// Only works when run as Admin, or Debugged as Admin public static void ResetThreadName(Thread thread, string newName) { lock (_lockObject) { var field = thread.GetType().GetField("m_Name", BindingFlags.Instance | BindingFlags.NonPublic); if (null != field) { field.SetValue(thread, null); thread.Name = null; } } thread.Name = newName; }
public static bool _TerminateUnmanagedThread(this Thread thread) { MethodInfo GetNativeHandle = thread.GetType().GetMethod("GetNativeHandle", BindingFlags.NonPublic | BindingFlags.Instance); if (GetNativeHandle != null) { object threadHandle = GetNativeHandle.Invoke(thread, new object[] { }); if (threadHandle != null) { object m_ptr = GetInstanceField(threadHandle.GetType(), threadHandle, "m_ptr"); if (m_ptr != null) { Win32.SuspendThread((IntPtr)m_ptr); //Win32.TerminateThread((IntPtr) m_ptr, 0); } } } return(false); }