mscorlib(4.0.0.0) API with additions
ProcessThread.cs
1 using Microsoft.Win32;
2 using Microsoft.Win32.SafeHandles;
6 
7 namespace System.Diagnostics
8 {
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)]
12  public class ProcessThread : Component
13  {
14  private enum State
15  {
16  IsLocal = 2,
17  IsNt = 4
18  }
19 
20  private ThreadInfo threadInfo;
21 
22  private bool isRemoteMachine;
23 
24  private bool priorityBoostEnabled;
25 
26  private bool havePriorityBoostEnabled;
27 
28  private ThreadPriorityLevel priorityLevel;
29 
30  private bool havePriorityLevel;
31 
34  [MonitoringDescription("ThreadBasePriority")]
35  public int BasePriority
36  {
37  get
38  {
39  return threadInfo.basePriority;
40  }
41  }
42 
45  [MonitoringDescription("ThreadCurrentPriority")]
46  public int CurrentPriority
47  {
48  get
49  {
50  return threadInfo.currentPriority;
51  }
52  }
53 
56  [MonitoringDescription("ThreadId")]
57  public int Id
58  {
59  get
60  {
61  return threadInfo.threadId;
62  }
63  }
64 
70  [Browsable(false)]
71  public int IdealProcessor
72  {
73  set
74  {
75  Microsoft.Win32.SafeHandles.SafeThreadHandle handle = null;
76  try
77  {
78  handle = OpenThreadHandle(32);
79  if (Microsoft.Win32.NativeMethods.SetThreadIdealProcessor(handle, value) < 0)
80  {
81  throw new Win32Exception();
82  }
83  }
84  finally
85  {
86  CloseThreadHandle(handle);
87  }
88  }
89  }
90 
97  [MonitoringDescription("ThreadPriorityBoostEnabled")]
98  public bool PriorityBoostEnabled
99  {
100  get
101  {
102  if (!havePriorityBoostEnabled)
103  {
104  Microsoft.Win32.SafeHandles.SafeThreadHandle handle = null;
105  try
106  {
107  handle = OpenThreadHandle(64);
108  bool disabled = false;
109  if (!Microsoft.Win32.NativeMethods.GetThreadPriorityBoost(handle, out disabled))
110  {
111  throw new Win32Exception();
112  }
113  priorityBoostEnabled = !disabled;
114  havePriorityBoostEnabled = true;
115  }
116  finally
117  {
118  CloseThreadHandle(handle);
119  }
120  }
121  return priorityBoostEnabled;
122  }
123  set
124  {
125  Microsoft.Win32.SafeHandles.SafeThreadHandle handle = null;
126  try
127  {
128  handle = OpenThreadHandle(32);
129  if (!Microsoft.Win32.NativeMethods.SetThreadPriorityBoost(handle, !value))
130  {
131  throw new Win32Exception();
132  }
133  priorityBoostEnabled = value;
134  havePriorityBoostEnabled = true;
135  }
136  finally
137  {
138  CloseThreadHandle(handle);
139  }
140  }
141  }
142 
148  [MonitoringDescription("ThreadPriorityLevel")]
150  {
151  get
152  {
153  if (!havePriorityLevel)
154  {
155  Microsoft.Win32.SafeHandles.SafeThreadHandle handle = null;
156  try
157  {
158  handle = OpenThreadHandle(64);
159  int threadPriority = Microsoft.Win32.NativeMethods.GetThreadPriority(handle);
160  if (threadPriority == int.MaxValue)
161  {
162  throw new Win32Exception();
163  }
164  priorityLevel = (ThreadPriorityLevel)threadPriority;
165  havePriorityLevel = true;
166  }
167  finally
168  {
169  CloseThreadHandle(handle);
170  }
171  }
172  return priorityLevel;
173  }
174  set
175  {
176  Microsoft.Win32.SafeHandles.SafeThreadHandle handle = null;
177  try
178  {
179  handle = OpenThreadHandle(32);
180  if (!Microsoft.Win32.NativeMethods.SetThreadPriority(handle, (int)value))
181  {
182  throw new Win32Exception();
183  }
184  priorityLevel = value;
185  }
186  finally
187  {
188  CloseThreadHandle(handle);
189  }
190  }
191  }
192 
198  [MonitoringDescription("ThreadPrivilegedProcessorTime")]
200  {
201  get
202  {
203  EnsureState(State.IsNt);
204  return GetThreadTimes().PrivilegedProcessorTime;
205  }
206  }
207 
212  [MonitoringDescription("ThreadStartAddress")]
213  public IntPtr StartAddress
214  {
215  get
216  {
217  EnsureState(State.IsNt);
218  return threadInfo.startAddress;
219  }
220  }
221 
227  [MonitoringDescription("ThreadStartTime")]
228  public DateTime StartTime
229  {
230  get
231  {
232  EnsureState(State.IsNt);
233  return GetThreadTimes().StartTime;
234  }
235  }
236 
241  [MonitoringDescription("ThreadThreadState")]
242  public ThreadState ThreadState
243  {
244  get
245  {
246  EnsureState(State.IsNt);
247  return threadInfo.threadState;
248  }
249  }
250 
256  [MonitoringDescription("ThreadTotalProcessorTime")]
258  {
259  get
260  {
261  EnsureState(State.IsNt);
262  return GetThreadTimes().TotalProcessorTime;
263  }
264  }
265 
271  [MonitoringDescription("ThreadUserProcessorTime")]
273  {
274  get
275  {
276  EnsureState(State.IsNt);
277  return GetThreadTimes().UserProcessorTime;
278  }
279  }
280 
286  [MonitoringDescription("ThreadWaitReason")]
288  {
289  get
290  {
291  EnsureState(State.IsNt);
292  if (threadInfo.threadState != ThreadState.Wait)
293  {
294  throw new InvalidOperationException(SR.GetString("WaitReasonUnavailable"));
295  }
296  return threadInfo.threadWaitReason;
297  }
298  }
299 
305  [Browsable(false)]
307  {
308  set
309  {
310  Microsoft.Win32.SafeHandles.SafeThreadHandle handle = null;
311  try
312  {
313  handle = OpenThreadHandle(96);
314  if (Microsoft.Win32.NativeMethods.SetThreadAffinityMask(handle, new HandleRef(this, value)) == IntPtr.Zero)
315  {
316  throw new Win32Exception();
317  }
318  }
319  finally
320  {
321  CloseThreadHandle(handle);
322  }
323  }
324  }
325 
326  internal ProcessThread(bool isRemoteMachine, ThreadInfo threadInfo)
327  {
328  this.isRemoteMachine = isRemoteMachine;
329  this.threadInfo = threadInfo;
330  GC.SuppressFinalize(this);
331  }
332 
333  private static void CloseThreadHandle(Microsoft.Win32.SafeHandles.SafeThreadHandle handle)
334  {
335  handle?.Close();
336  }
337 
338  private void EnsureState(State state)
339  {
340  if ((state & State.IsLocal) != 0 && isRemoteMachine)
341  {
342  throw new NotSupportedException(SR.GetString("NotSupportedRemoteThread"));
343  }
344  if ((state & State.IsNt) != 0 && Environment.OSVersion.Platform != PlatformID.Win32NT)
345  {
346  throw new PlatformNotSupportedException(SR.GetString("WinNTRequired"));
347  }
348  }
349 
350  private Microsoft.Win32.SafeHandles.SafeThreadHandle OpenThreadHandle(int access)
351  {
352  EnsureState(State.IsLocal);
353  return ProcessManager.OpenThread(threadInfo.threadId, access);
354  }
355 
360  public void ResetIdealProcessor()
361  {
362  IdealProcessor = 32;
363  }
364 
365  private ProcessThreadTimes GetThreadTimes()
366  {
367  ProcessThreadTimes processThreadTimes = new ProcessThreadTimes();
368  Microsoft.Win32.SafeHandles.SafeThreadHandle handle = null;
369  try
370  {
371  handle = OpenThreadHandle(64);
372  if (!Microsoft.Win32.NativeMethods.GetThreadTimes(handle, out processThreadTimes.create, out processThreadTimes.exit, out processThreadTimes.kernel, out processThreadTimes.user))
373  {
374  throw new Win32Exception();
375  }
376  return processThreadTimes;
377  }
378  finally
379  {
380  CloseThreadHandle(handle);
381  }
382  }
383  }
384 }
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.
Definition: PlatformID.cs:8
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.
Definition: GC.cs:308
Definition: __Canon.cs:3
Wraps a managed object holding a handle to a resource that is passed to unmanaged code using platform...
Definition: HandleRef.cs:5
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 ....
Definition: DateTime.cs:13
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.
Definition: IntPtr.cs:14
Provides the base implementation for the T:System.ComponentModel.IComponent interface and enables obj...
Definition: Component.cs:9
ThreadPriorityLevel PriorityLevel
Gets or sets the priority level of the thread.
Controls the system garbage collector, a service that automatically reclaims unused memory.
Definition: GC.cs:11
ThreadState
Specifies the current execution state of the thread.
Definition: ThreadState.cs:4
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.
Definition: IntPtr.cs:20
Represents a time interval.To browse the .NET Framework source code for this type,...
Definition: TimeSpan.cs:12
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.