Terraria
v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ProcessThread.cs
Go to the documentation of this file.
1
using
System.ComponentModel
;
2
using
System.Runtime.Versioning
;
3
using
Microsoft.Win32.SafeHandles
;
4
5
namespace
System.Diagnostics
;
6
7
[Designer(
"System.Diagnostics.Design.ProcessThreadDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
)]
8
public
class
ProcessThread
:
Component
9
{
10
private
enum
State
11
{
12
IsLocal
= 2
13
}
14
15
private
readonly
bool
_isRemoteMachine
;
16
17
private
readonly
int
_processId
;
18
19
private
readonly
ThreadInfo
_threadInfo
;
20
21
private
bool
?
_priorityBoostEnabled
;
22
23
private
ThreadPriorityLevel
?
_priorityLevel
;
24
25
public
int
BasePriority
=>
_threadInfo
.
_basePriority
;
26
27
public
int
CurrentPriority
=>
_threadInfo
.
_currentPriority
;
28
29
public
int
Id
=> (int)
_threadInfo
.
_threadId
;
30
31
public
bool
PriorityBoostEnabled
32
{
33
get
34
{
35
if
(!
_priorityBoostEnabled
.HasValue)
36
{
37
_priorityBoostEnabled
=
PriorityBoostEnabledCore
;
38
}
39
return
_priorityBoostEnabled
.Value;
40
}
41
set
42
{
43
PriorityBoostEnabledCore
=
value
;
44
_priorityBoostEnabled
=
value
;
45
}
46
}
47
48
public
ThreadPriorityLevel
PriorityLevel
49
{
50
[SupportedOSPlatform(
"windows"
)]
51
[SupportedOSPlatform(
"linux"
)]
52
[SupportedOSPlatform(
"freebsd"
)]
53
get
54
{
55
if
(!
_priorityLevel
.HasValue)
56
{
57
_priorityLevel
=
PriorityLevelCore
;
58
}
59
return
_priorityLevel
.Value;
60
}
61
[SupportedOSPlatform(
"windows"
)]
62
set
63
{
64
PriorityLevelCore
=
value
;
65
_priorityLevel
=
value
;
66
}
67
}
68
69
public
IntPtr
StartAddress
=>
_threadInfo
.
_startAddress
;
70
71
public
ThreadState
ThreadState
=>
_threadInfo
.
_threadState
;
72
73
public
ThreadWaitReason
WaitReason
74
{
75
get
76
{
77
if
(
_threadInfo
.
_threadState
!=
ThreadState
.Wait)
78
{
79
throw
new
InvalidOperationException
(
System
.
SR
.
WaitReasonUnavailable
);
80
}
81
return
_threadInfo
.
_threadWaitReason
;
82
}
83
}
84
85
[SupportedOSPlatform(
"windows"
)]
86
[SupportedOSPlatform(
"linux"
)]
87
public
DateTime
StartTime
=>
GetStartTime
();
88
89
public
int
IdealProcessor
90
{
91
set
92
{
93
using
SafeThreadHandle
handle
=
OpenThreadHandle
(32);
94
if
(global::Interop.Kernel32.SetThreadIdealProcessor(
handle
,
value
) < 0)
95
{
96
throw
new
Win32Exception
();
97
}
98
}
99
}
100
101
private
bool
PriorityBoostEnabledCore
102
{
103
get
104
{
105
using
SafeThreadHandle
handle
=
OpenThreadHandle
(64);
106
if
(!global::Interop.Kernel32.GetThreadPriorityBoost(
handle
, out var disabled))
107
{
108
throw
new
Win32Exception
();
109
}
110
return
!disabled;
111
}
112
set
113
{
114
using
SafeThreadHandle
handle
=
OpenThreadHandle
(32);
115
if
(!global::Interop.Kernel32.SetThreadPriorityBoost(
handle
, !
value
))
116
{
117
throw
new
Win32Exception
();
118
}
119
}
120
}
121
122
private
ThreadPriorityLevel
PriorityLevelCore
123
{
124
get
125
{
126
using
SafeThreadHandle
handle
=
OpenThreadHandle
(64);
127
int
threadPriority = global::Interop.Kernel32.GetThreadPriority(
handle
);
128
if
(threadPriority ==
int
.MaxValue)
129
{
130
throw
new
Win32Exception
();
131
}
132
return
(
ThreadPriorityLevel
)threadPriority;
133
}
134
set
135
{
136
using
SafeThreadHandle
handle
=
OpenThreadHandle
(32);
137
if
(!global::Interop.Kernel32.SetThreadPriority(
handle
, (
int
)
value
))
138
{
139
throw
new
Win32Exception
();
140
}
141
}
142
}
143
144
[SupportedOSPlatform(
"windows"
)]
145
public
IntPtr
ProcessorAffinity
146
{
147
set
148
{
149
using
SafeThreadHandle
handle
=
OpenThreadHandle
(96);
150
if
(global::Interop.Kernel32.SetThreadAffinityMask(
handle
,
value
) ==
IntPtr
.
Zero
)
151
{
152
throw
new
Win32Exception
();
153
}
154
}
155
}
156
157
public
TimeSpan
PrivilegedProcessorTime
=>
GetThreadTimes
().
PrivilegedProcessorTime
;
158
159
public
TimeSpan
TotalProcessorTime
=>
GetThreadTimes
().
TotalProcessorTime
;
160
161
public
TimeSpan
UserProcessorTime
=>
GetThreadTimes
().
UserProcessorTime
;
162
163
internal
ProcessThread
(
bool
isRemoteMachine,
int
processId,
ThreadInfo
threadInfo)
164
{
165
_isRemoteMachine
= isRemoteMachine;
166
_processId
= processId;
167
_threadInfo
= threadInfo;
168
}
169
170
private
void
EnsureState
(
State
state
)
171
{
172
if
((
state
&
State
.IsLocal) != 0 &&
_isRemoteMachine
)
173
{
174
throw
new
NotSupportedException
(
System
.
SR
.
NotSupportedRemoteThread
);
175
}
176
}
177
178
public
void
ResetIdealProcessor
()
179
{
180
int
idealProcessor = ((IntPtr.Size == 4) ? 32 : 64);
181
IdealProcessor
= idealProcessor;
182
}
183
184
private
DateTime
GetStartTime
()
185
{
186
return
GetThreadTimes
().
StartTime
;
187
}
188
189
private
ProcessThreadTimes
GetThreadTimes
()
190
{
191
using
SafeThreadHandle
handle
=
OpenThreadHandle
(64);
192
ProcessThreadTimes
processThreadTimes =
new
ProcessThreadTimes
();
193
if
(!global::Interop.Kernel32.GetThreadTimes(
handle
, out processThreadTimes.
_create
, out processThreadTimes.
_exit
, out processThreadTimes.
_kernel
, out processThreadTimes.
_user
))
194
{
195
throw
new
Win32Exception
();
196
}
197
return
processThreadTimes;
198
}
199
200
private
SafeThreadHandle
OpenThreadHandle
(
int
access)
201
{
202
EnsureState
(
State
.IsLocal);
203
return
ProcessManager
.
OpenThread
((
int
)
_threadInfo
.
_threadId
, access);
204
}
205
}
Microsoft.Win32.SafeHandles.SafeThreadHandle
Definition
SafeThreadHandle.cs:7
System.ComponentModel.Component
Definition
Component.cs:5
System.Diagnostics.ProcessManager.OpenThread
static SafeThreadHandle OpenThread(int threadId, int access)
Definition
ProcessManager.cs:156
System.Diagnostics.ProcessManager
Definition
ProcessManager.cs:9
System.Diagnostics.ProcessThreadTimes._user
long _user
Definition
ProcessThreadTimes.cs:11
System.Diagnostics.ProcessThreadTimes._kernel
long _kernel
Definition
ProcessThreadTimes.cs:9
System.Diagnostics.ProcessThreadTimes.UserProcessorTime
TimeSpan UserProcessorTime
Definition
ProcessThreadTimes.cs:19
System.Diagnostics.ProcessThreadTimes._exit
long _exit
Definition
ProcessThreadTimes.cs:7
System.Diagnostics.ProcessThreadTimes.TotalProcessorTime
TimeSpan TotalProcessorTime
Definition
ProcessThreadTimes.cs:21
System.Diagnostics.ProcessThreadTimes.StartTime
DateTime StartTime
Definition
ProcessThreadTimes.cs:13
System.Diagnostics.ProcessThreadTimes.PrivilegedProcessorTime
TimeSpan PrivilegedProcessorTime
Definition
ProcessThreadTimes.cs:17
System.Diagnostics.ProcessThreadTimes._create
long _create
Definition
ProcessThreadTimes.cs:5
System.Diagnostics.ProcessThreadTimes
Definition
ProcessThreadTimes.cs:4
System.Diagnostics.ProcessThread.EnsureState
void EnsureState(State state)
Definition
ProcessThread.cs:170
System.Diagnostics.ProcessThread._priorityBoostEnabled
bool? _priorityBoostEnabled
Definition
ProcessThread.cs:21
System.Diagnostics.ProcessThread.ProcessThread
ProcessThread(bool isRemoteMachine, int processId, ThreadInfo threadInfo)
Definition
ProcessThread.cs:163
System.Diagnostics.ProcessThread.PriorityBoostEnabledCore
bool PriorityBoostEnabledCore
Definition
ProcessThread.cs:102
System.Diagnostics.ProcessThread.PrivilegedProcessorTime
TimeSpan PrivilegedProcessorTime
Definition
ProcessThread.cs:157
System.Diagnostics.ProcessThread.CurrentPriority
int CurrentPriority
Definition
ProcessThread.cs:27
System.Diagnostics.ProcessThread.IdealProcessor
int IdealProcessor
Definition
ProcessThread.cs:90
System.Diagnostics.ProcessThread.ResetIdealProcessor
void ResetIdealProcessor()
Definition
ProcessThread.cs:178
System.Diagnostics.ProcessThread.PriorityLevel
ThreadPriorityLevel PriorityLevel
Definition
ProcessThread.cs:49
System.Diagnostics.ProcessThread._priorityLevel
ThreadPriorityLevel? _priorityLevel
Definition
ProcessThread.cs:23
System.Diagnostics.ProcessThread.WaitReason
ThreadWaitReason WaitReason
Definition
ProcessThread.cs:74
System.Diagnostics.ProcessThread.State
State
Definition
ProcessThread.cs:11
System.Diagnostics.ProcessThread.State.IsLocal
@ IsLocal
System.Diagnostics.ProcessThread.PriorityBoostEnabled
bool PriorityBoostEnabled
Definition
ProcessThread.cs:32
System.Diagnostics.ProcessThread.PriorityLevelCore
ThreadPriorityLevel PriorityLevelCore
Definition
ProcessThread.cs:123
System.Diagnostics.ProcessThread._threadInfo
readonly ThreadInfo _threadInfo
Definition
ProcessThread.cs:19
System.Diagnostics.ProcessThread.StartTime
DateTime StartTime
Definition
ProcessThread.cs:87
System.Diagnostics.ProcessThread.Id
int Id
Definition
ProcessThread.cs:29
System.Diagnostics.ProcessThread.GetThreadTimes
ProcessThreadTimes GetThreadTimes()
Definition
ProcessThread.cs:189
System.Diagnostics.ProcessThread.ProcessorAffinity
IntPtr ProcessorAffinity
Definition
ProcessThread.cs:146
System.Diagnostics.ProcessThread.BasePriority
int BasePriority
Definition
ProcessThread.cs:25
System.Diagnostics.ProcessThread.StartAddress
IntPtr StartAddress
Definition
ProcessThread.cs:69
System.Diagnostics.ProcessThread.GetStartTime
DateTime GetStartTime()
Definition
ProcessThread.cs:184
System.Diagnostics.ProcessThread.OpenThreadHandle
SafeThreadHandle OpenThreadHandle(int access)
Definition
ProcessThread.cs:200
System.Diagnostics.ProcessThread.UserProcessorTime
TimeSpan UserProcessorTime
Definition
ProcessThread.cs:161
System.Diagnostics.ProcessThread.TotalProcessorTime
TimeSpan TotalProcessorTime
Definition
ProcessThread.cs:159
System.Diagnostics.ProcessThread._processId
readonly int _processId
Definition
ProcessThread.cs:17
System.Diagnostics.ProcessThread._isRemoteMachine
readonly bool _isRemoteMachine
Definition
ProcessThread.cs:15
System.Diagnostics.ProcessThread
Definition
ProcessThread.cs:9
System.Diagnostics.ThreadInfo._threadState
ThreadState _threadState
Definition
ThreadInfo.cs:15
System.Diagnostics.ThreadInfo._startAddress
IntPtr _startAddress
Definition
ThreadInfo.cs:13
System.Diagnostics.ThreadInfo._currentPriority
int _currentPriority
Definition
ThreadInfo.cs:11
System.Diagnostics.ThreadInfo._threadWaitReason
ThreadWaitReason _threadWaitReason
Definition
ThreadInfo.cs:17
System.Diagnostics.ThreadInfo._basePriority
int _basePriority
Definition
ThreadInfo.cs:9
System.Diagnostics.ThreadInfo._threadId
ulong _threadId
Definition
ThreadInfo.cs:5
System.Diagnostics.ThreadInfo
Definition
ThreadInfo.cs:4
System.InvalidOperationException
Definition
InvalidOperationException.cs:9
System.NotSupportedException
Definition
NotSupportedException.cs:9
System.SR.WaitReasonUnavailable
static string WaitReasonUnavailable
Definition
SR.cs:40
System.SR.NotSupportedRemoteThread
static string NotSupportedRemoteThread
Definition
SR.cs:42
System.SR
Definition
SR.cs:7
Win32Exception
Microsoft.Win32.SafeHandles
Definition
SafeProcessHandle.cs:3
System.ComponentModel
Definition
ColumnAttribute.cs:3
System.Diagnostics.ThreadPriorityLevel
ThreadPriorityLevel
Definition
ThreadPriorityLevel.cs:4
System.Diagnostics.ThreadState
ThreadState
Definition
ThreadState.cs:4
System.Diagnostics.ThreadWaitReason
ThreadWaitReason
Definition
ThreadWaitReason.cs:4
System.Diagnostics
Definition
AggregationManager.cs:6
System.Runtime.Versioning
Definition
NonVersionableAttribute.cs:1
System.ExceptionArgument.value
@ value
System.ExceptionArgument.state
@ state
System.ExceptionArgument.handle
@ handle
System
Definition
BlockingCollection.cs:8
System.DateTime
Definition
DateTime.cs:15
System.IntPtr.Zero
static readonly IntPtr Zero
Definition
IntPtr.cs:18
System.IntPtr
Definition
IntPtr.cs:14
System.TimeSpan
Definition
TimeSpan.cs:10
source
System.Diagnostics.Process
System.Diagnostics
ProcessThread.cs
Generated by
1.10.0