-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathuThreadChecker.pas
211 lines (188 loc) · 5.86 KB
/
uThreadChecker.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
unit uThreadChecker;
interface
uses
System.Classes, Winapi.tlHelp32, Winapi.Windows, System.SysUtils,
Winapi.Shellapi, Vcl.ComCtrls,
Winapi.Messages, Vcl.Forms, Vcl.Graphics, uIdiomas;
type
HChecker = class(TThread)
private
procedure CheckAndKill;
{ Private declarations }
protected
procedure Execute; override;
end;
var
VirtualList: TList;
implementation
uses
uUOS;
// Simple mataprocesos por nombre
Procedure KillProcessByName(ProcessName: String);
var
ProcessEntry: TProcessEntry32;
hSnapshot: THandle;
begin
hSnapshot:= CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
ProcessEntry.dwSize:= SizeOf(ProcessEntry32);
Process32First(hSnapshot, ProcessEntry);
while Process32Next(hSnapshot, ProcessEntry) do
if ProcessEntry.szExeFile = ProcessName then
TerminateProcess(OpenProcess(PROCESS_TERMINATE, False, ProcessEntry.th32ProcessID), 0);
CloseHandle(hSnapshot);
end;
// Procedimiento para ejecutar, matar procesos y errores
Procedure HChecker.CheckAndKill;
var
TSFuncionales: TStringList;
i, o: integer;
TotalFiles, Espera: integer;
OFuncionales, Max: integer;
FicheroActual, RutaCompleta, Dir, Funcionales: String;
hFindW: HWND;
begin
OFuncionales:= 0;
if Length(Form1.Edit5.Text) = 0 then
Max:= 0
else
Max:= StrToInt(Form1.Edit5.Text);
Dir:= Form1.EdDir.Text;
Funcionales:= Dir + '\' + Form1.EdFuncionales.Text;
if Form1.EdEspera.Text = '' then
Espera:= 750 // Default
else
Espera:= StrToInt(Form1.EdEspera.Text);
if FileExists(Funcionales) then
DeleteFile(Funcionales);
TotalFiles:= Form1.ListView2.Items.Count;
if TotalFiles = 0 then
begin
Form1.Estado.Caption:= Var5;
Exit;
end;
for i:= 0 to TotalFiles - 1 do
begin
if Terminated then
Exit;
if Form1.CheckBox1.Checked then
if Max = OFuncionales then
Break;
FicheroActual:= ExtractFileName(Form1.ListView2.Items.Item[i].Caption);
RutaCompleta:= Form1.ListView2.Items.Item[i].Caption;
Form1.Estado.Caption:= Var6 + ' ' + FicheroActual;
ShellExecute(0, 'open', PChar(RutaCompleta), nil, nil, SW_SHOW);
Sleep(Espera);
if not Form1.ChkRevFinal.Checked then
begin
if FileExists(Funcionales) then
begin
while FileExists(Funcionales) do
DeleteFile(Funcionales);
TListItem(VirtualList[i]).SubItems[0]:= var7;
Application.ProcessMessages;
inc(OFuncionales);
end
else
begin
TListItem(VirtualList[i]).SubItems[0]:= Var8;
Application.ProcessMessages;
end;
end
else
begin
TListItem(VirtualList[i]).SubItems[0]:= Var9;
Application.ProcessMessages;
end;
if Form1.CheckBox2.Checked then // Scroll ListView2
begin
Form1.ListView2.Items.Item[i].Selected:= True;
Form1.ListView2.SetFocus;
if i > 10 then
Form1.ListView2.Scroll(0, Abs(Form1.ListView2.Font.Height));
Application.ProcessMessages;
end;
hFindW:= 0;
hFindW:= FindWindow('#32770', nil);
if hFindW <> 0 then
begin
PostMessage(hFindW, WM_CLOSE, 0, 0);
PostMessage(hFindW, WM_QUIT, 0, 0);
end;
KillProcessByName(FicheroActual);
end;
if Form1.ChkRevFinal.Checked then
try
TSFuncionales:= TStringList.Create;
try
TSFuncionales.LoadFromFile(Funcionales);
for i:= 0 to Form1.ListView2.Items.Count - 1 do
for o:= 0 to TSFuncionales.Count - 1 do
begin
if pos(TSFuncionales.Strings[o], Form1.ListView2.Items.Item[i].Caption) <> 0 then
begin
TListItem(VirtualList[i]).SubItems[0]:= var7;
inc(OFuncionales);
Application.ProcessMessages;
Break;
end
else
begin
TListItem(VirtualList[i]).SubItems[0]:= Var8;
Application.ProcessMessages;
end;
end;
except
for i:= 0 to Form1.ListView2.Items.Count - 1 do
begin
TListItem(VirtualList[i]).SubItems[0]:= Var8;
Application.ProcessMessages;
end;
end;
finally
TSFuncionales.Free;
end;
Form1.ListView2.Repaint;
// Le damos unas pasadas por si algún cabrón no se cerró
for o:= 1 to 5 do
for i:= 0 to TotalFiles - 1 do
begin
if Terminated then
Exit;
Form1.Estado.Caption:= Var10;
Application.ProcessMessages;
if Form1.ListView2.Items.Item[i].SubItems[0] <> '' then
begin
FicheroActual:= ExtractFileName(Form1.ListView2.Items.Item[i].Caption);
hFindW:= 0;
hFindW:= FindWindow('#32770', nil);
if hFindW <> 0 then
begin
PostMessage(hFindW, WM_CLOSE, 0, 0);
PostMessage(hFindW, WM_QUIT, 0, 0);
end;
KillProcessByName(FicheroActual);
end;
end;
// Si está marcado, eliminamos las no funcionales
if Form1.ChkElimNoF.Checked then
for i:= 0 to TotalFiles - 1 do
if Form1.ListView2.Items.Item[i].SubItems[0] = Var8 then
begin
DeleteFile(Form1.ListView2.Items.Item[i].Caption);
Form1.Estado.Caption:= Var11;
Application.ProcessMessages;
end;
if NOT Form1.CheckBox1.Checked then
Form1.Estado.Caption:= Var12 + ' ' + IntToStr(OFuncionales) + ' ' + Var13
else if OFuncionales = Max then
Form1.Estado.Caption:= Var14 + ' ' + IntToStr(OFuncionales) + ' ' + Var13
else
Form1.Estado.Caption:= Var12 + ' ' + IntToStr(OFuncionales) + ' ' + Var13;
end;
{ HChecker }
procedure HChecker.Execute;
begin
Synchronize(CheckAndKill);
{ Place thread code here }
end;
end.