2 using Microsoft.Win32.SafeHandles;
10 [Designer(
"System.Diagnostics.Design.ProcessThreadDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
11 [HostProtection(
SecurityAction.LinkDemand, SelfAffectingProcessMgmt =
true, SelfAffectingThreading =
true)]
20 private ThreadInfo threadInfo;
22 private bool isRemoteMachine;
24 private bool priorityBoostEnabled;
26 private bool havePriorityBoostEnabled;
30 private bool havePriorityLevel;
34 [MonitoringDescription(
"ThreadBasePriority")]
39 return threadInfo.basePriority;
45 [MonitoringDescription(
"ThreadCurrentPriority")]
50 return threadInfo.currentPriority;
56 [MonitoringDescription(
"ThreadId")]
61 return threadInfo.threadId;
75 Microsoft.Win32.SafeHandles.SafeThreadHandle handle =
null;
78 handle = OpenThreadHandle(32);
79 if (Microsoft.Win32.NativeMethods.SetThreadIdealProcessor(handle, value) < 0)
86 CloseThreadHandle(handle);
97 [MonitoringDescription(
"ThreadPriorityBoostEnabled")]
102 if (!havePriorityBoostEnabled)
104 Microsoft.Win32.SafeHandles.SafeThreadHandle handle =
null;
107 handle = OpenThreadHandle(64);
108 bool disabled =
false;
109 if (!Microsoft.Win32.NativeMethods.GetThreadPriorityBoost(handle, out disabled))
113 priorityBoostEnabled = !disabled;
114 havePriorityBoostEnabled =
true;
118 CloseThreadHandle(handle);
121 return priorityBoostEnabled;
125 Microsoft.Win32.SafeHandles.SafeThreadHandle handle =
null;
128 handle = OpenThreadHandle(32);
129 if (!Microsoft.Win32.NativeMethods.SetThreadPriorityBoost(handle, !value))
133 priorityBoostEnabled = value;
134 havePriorityBoostEnabled =
true;
138 CloseThreadHandle(handle);
148 [MonitoringDescription(
"ThreadPriorityLevel")]
153 if (!havePriorityLevel)
155 Microsoft.Win32.SafeHandles.SafeThreadHandle handle =
null;
158 handle = OpenThreadHandle(64);
159 int threadPriority = Microsoft.Win32.NativeMethods.GetThreadPriority(handle);
160 if (threadPriority ==
int.MaxValue)
165 havePriorityLevel =
true;
169 CloseThreadHandle(handle);
172 return priorityLevel;
176 Microsoft.Win32.SafeHandles.SafeThreadHandle handle =
null;
179 handle = OpenThreadHandle(32);
180 if (!Microsoft.Win32.NativeMethods.SetThreadPriority(handle, (
int)value))
184 priorityLevel = value;
188 CloseThreadHandle(handle);
198 [MonitoringDescription(
"ThreadPrivilegedProcessorTime")]
203 EnsureState(State.IsNt);
204 return GetThreadTimes().PrivilegedProcessorTime;
212 [MonitoringDescription(
"ThreadStartAddress")]
217 EnsureState(State.IsNt);
218 return threadInfo.startAddress;
227 [MonitoringDescription(
"ThreadStartTime")]
232 EnsureState(State.IsNt);
233 return GetThreadTimes().StartTime;
241 [MonitoringDescription(
"ThreadThreadState")]
246 EnsureState(State.IsNt);
247 return threadInfo.threadState;
256 [MonitoringDescription(
"ThreadTotalProcessorTime")]
261 EnsureState(State.IsNt);
262 return GetThreadTimes().TotalProcessorTime;
271 [MonitoringDescription(
"ThreadUserProcessorTime")]
276 EnsureState(State.IsNt);
277 return GetThreadTimes().UserProcessorTime;
286 [MonitoringDescription(
"ThreadWaitReason")]
291 EnsureState(State.IsNt);
296 return threadInfo.threadWaitReason;
310 Microsoft.Win32.SafeHandles.SafeThreadHandle handle =
null;
313 handle = OpenThreadHandle(96);
314 if (Microsoft.Win32.NativeMethods.SetThreadAffinityMask(handle,
new HandleRef(
this, value)) ==
IntPtr.
Zero)
321 CloseThreadHandle(handle);
326 internal ProcessThread(
bool isRemoteMachine, ThreadInfo threadInfo)
328 this.isRemoteMachine = isRemoteMachine;
329 this.threadInfo = threadInfo;
333 private static void CloseThreadHandle(Microsoft.Win32.SafeHandles.SafeThreadHandle handle)
338 private void EnsureState(State state)
340 if ((state & State.IsLocal) != 0 && isRemoteMachine)
342 throw new NotSupportedException(SR.GetString(
"NotSupportedRemoteThread"));
344 if ((state & State.IsNt) != 0 && Environment.OSVersion.Platform !=
PlatformID.Win32NT)
346 throw new PlatformNotSupportedException(SR.GetString(
"WinNTRequired"));
350 private Microsoft.Win32.SafeHandles.SafeThreadHandle OpenThreadHandle(
int access)
352 EnsureState(State.IsLocal);
353 return ProcessManager.OpenThread(threadInfo.threadId, access);
365 private ProcessThreadTimes GetThreadTimes()
367 ProcessThreadTimes processThreadTimes =
new ProcessThreadTimes();
368 Microsoft.Win32.SafeHandles.SafeThreadHandle handle =
null;
371 handle = OpenThreadHandle(64);
372 if (!Microsoft.Win32.NativeMethods.GetThreadTimes(handle, out processThreadTimes.create, out processThreadTimes.exit, out processThreadTimes.kernel, out processThreadTimes.user))
376 return processThreadTimes;
380 CloseThreadHandle(handle);
ThreadPriorityLevel
Specifies the priority level of a thread.
int CurrentPriority
Gets the current priority of the thread.
PlatformID
Identifies the operating system, or platform, supported by an assembly.
TimeSpan PrivilegedProcessorTime
Gets the amount of time that the thread has spent running code inside the operating system core.
int Id
Gets the unique identifier of the thread.
IntPtr StartAddress
Gets the memory address of the function that the operating system called that started this thread.
TimeSpan UserProcessorTime
Gets the amount of time that the associated thread has spent running code inside the application.
Represents an operating system process thread.
static void SuppressFinalize(object obj)
Requests that the common language runtime not call the finalizer for the specified object.
Wraps a managed object holding a handle to a resource that is passed to unmanaged code using platform...
DateTime StartTime
Gets the time that the operating system started the thread.
Represents an instant in time, typically expressed as a date and time of day. To browse the ....
int IdealProcessor
Sets the preferred processor for this thread to run on.
IntPtr ProcessorAffinity
Sets the processors on which the associated thread can run.
int BasePriority
Gets the base priority of the thread.
SecurityAction
Specifies the security actions that can be performed using declarative security.
Throws an exception for a Win32 error code.
void ResetIdealProcessor()
Resets the ideal processor for this thread to indicate that there is no single ideal processor....
A platform-specific type that is used to represent a pointer or a handle.
Provides the base implementation for the T:System.ComponentModel.IComponent interface and enables obj...
ThreadPriorityLevel PriorityLevel
Gets or sets the priority level of the thread.
Controls the system garbage collector, a service that automatically reclaims unused memory.
ThreadState
Specifies the current execution state of the thread.
bool PriorityBoostEnabled
Gets or sets a value indicating whether the operating system should temporarily boost the priority of...
ThreadWaitReason
Specifies the reason a thread is waiting.
static readonly IntPtr Zero
A read-only field that represents a pointer or handle that has been initialized to zero.
Represents a time interval.To browse the .NET Framework source code for this type,...
TimeSpan TotalProcessorTime
Gets the total amount of time that this thread has spent using the processor.
The exception that is thrown when a method call is invalid for the object's current state.
ThreadWaitReason WaitReason
Gets the reason that the thread is waiting.