-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathWorkerThreads.pas
101 lines (80 loc) · 2.19 KB
/
WorkerThreads.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
unit WorkerThreads;
interface
uses
System.Classes, Winapi.WinNt;
type
TPsSnapshotThread = class(TThread)
protected
procedure Execute; override;
end;
TTdSnapshotThread = class(TThread)
FOwner: TObject; // should be TFormProcessInfo
protected
procedure Execute; override;
public
constructor CreateOwned(Owner: TObject);
end;
// The main thread needs to post us an APC when it's time to terminate
procedure RequestShutdown(ApcArgument1, ApcArgument2, ApcArgument3: Pointer);
stdcall;
implementation
uses
PsSnapshot, TdSnapshot, MainForm, NtUtils.Threads, ProcessForm;
procedure RequestShutdown(ApcArgument1, ApcArgument2, ApcArgument3: Pointer);
stdcall;
begin
TThread.Current.Terminate;
end;
{ TPsSnapshotThread }
procedure TPsSnapshotThread.Execute;
var
Processes: TPsSnapshot;
begin
Priority := tpLower;
NtxSetNameThread(NtCurrentThread, 'Process Snapshotting');
repeat
// Perform snapshotting (might take some time)
Processes := TPsSnapshot.Create;
Processes.Snapshot;
// Notify the UI to update the process list
Synchronize(
procedure ()
begin
FormMain.ConsumeSnapshot(Processes);
end
);
Processes.Free;
// Sleep until it's time to snapshot again. We wait in an alertable state,
// therefore the main thread can wake us up to request termination.
NtxSleep(1000 * MILLISEC, True);
until Terminated;
end;
{ TTdSnapshotThread }
constructor TTdSnapshotThread.CreateOwned(Owner: TObject);
begin
inherited Create;
FOwner := Owner;
end;
procedure TTdSnapshotThread.Execute;
var
Owner: TFormProcessInfo;
Threads: TArray<TThreadData>;
begin
Priority := tpLower;
Owner := TFormProcessInfo(FOwner);
NtxSetNameThread(NtCurrentThread, 'Thread snapshotting for ' + Owner.Caption);
repeat
Threads := SnapshotThreads(Owner.PID);
// Notify the UI to update the process list
Synchronize(
procedure ()
begin
Owner.ConsumeSnapshot(Threads);
end
);
// Sleep until it's time to snapshot again. We wait in an alertable state,
// therefore the main thread can wake us up to request termination.
NtxSleep(1500 * MILLISEC, True);
until Terminated;
end;
end.