-
Notifications
You must be signed in to change notification settings - Fork 0
/
runonce_postit.pas
372 lines (320 loc) · 8.63 KB
/
runonce_postit.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
(*
RunOnce_PostIt allows your application to run only once.
If you try to run it again, you may post a message to the first running application.
It works for lcl, fpGUI, msei and console applications.
RunOnce procedure is (a lot of) inspired by LSRunOnce of LazSolutions, created
by Silvio Clecio : http://silvioprog.com.br
Fred van Stappen
fiens@hotmail.com
*)
unit RunOnce_PostIt;
{.$DEFINE fpgui} // uncomment if using fpgui
{.$DEFINE mse} // uncomment if using mse
interface
uses
{$IFDEF MSWINDOWS}
Windows, JwaTlHelp32,
{$ENDIF}
{$IF DEFINED(LCL)}
ExtCtrls, /// for lcl timer
{$endif}
{$IF DEFINED(fpgui)}
fpg_main, /// for fpgui timer
{$ENDIF}
{$IF DEFINED(mse)}
msetimer, /// for mse timer
{$ENDIF}
SysUtils, Classes, Process;
type
TProc = procedure of object;
type
TOncePost = class(TObject)
private
TheProc: TProc;
{$IF DEFINED(LCL)}
ATimer: TTimer; /// for lcl timer
procedure InitMessage(AOwner: TComponent);
{$endif}
{$IF DEFINED(fpgui)}
ATimer: Tfpgtimer; /// for fpGUI
procedure InitMessage;
{$endif}
{$IF DEFINED(mse)}
ATimer: TTimer; /// for fpGUI
procedure InitMessage;
{$endif}
function ExecProcess(const ACommandLine: string): string;
procedure ListProcess(const AProcess: TStringList; const AShowPID: boolean = False;
const ASorted: boolean = True; const AIgnoreCurrent: boolean = False);
function PostIt: string;
const
CLSUtilsProcessNameValueSeparator: char = '=';
{$IFDEF mse}
procedure onTimerPost(const sender: TObject);
{$else}
procedure onTimerPost(Sender: TObject);
{$endif}
procedure RunOnce(AMessage: string);
end;
procedure RunOnce(AMessage: string);
{$IF DEFINED(LCL)}
procedure InitMessage(AOwner: TComponent); /// LcL
{$endif}
{$IF DEFINED(fpgui)}
procedure InitMessage; /// fpgui
{$endif}
{$IF DEFINED(mse)}
procedure InitMessage; /// mse
{$endif}
procedure FreeRunOnce;
procedure StopMessage;
{$IFDEF mse}
procedure StartMessage(AProc: Tproc; const AInterval: integer = 1000000);
{$else}
procedure StartMessage(AProc: Tproc; const AInterval: integer = 1000);
{$endif}
var
TheOncePost: TOncePost;
TheMessage: string;
implementation
procedure RunOnce(AMessage: string);
begin
TheOncePost := TOncePost.Create;
TheOncePost.RunOnce(AMessage);
end;
{$IF DEFINED(LCL)}
procedure InitMessage(AOwner: TComponent); // lcl
begin
if assigned(TheOncePost.ATimer) = false then TheOncePost.InitMessage(AOwner);
end;
{$endif}
{$IF DEFINED(fpgui)}
procedure InitMessage ; /// fpgui
begin
if assigned(TheOncePost.ATimer) = false then TheOncePost.InitMessage;
end;
{$endif}
{$IF DEFINED(mse)}
procedure InitMessage ; /// mse
begin
if assigned(TheOncePost.ATimer) = false then TheOncePost.InitMessage;
end;
{$endif}
procedure StopMessage;
begin
if assigned(TheOncePost.ATimer) then
begin
TheOncePost.ATimer.Enabled:=false;
end;
end;
{$IFDEF mse}
procedure StartMessage(AProc: Tproc; const AInterval: integer = 1000000);
{$else}
procedure StartMessage(AProc: Tproc; const AInterval: integer = 1000);
{$endif}
begin
TheOncePost.ATimer.Enabled := false;
TheOncePost.TheProc := AProc;
TheOncePost.ATimer.Interval := AInterval;
TheOncePost.ATimer.OnTimer := @TheOncePost.onTimerPost;
TheOncePost.ATimer.Enabled := True;
end;
procedure FreeRunOnce;
begin
if assigned(TheOncePost.ATimer) then
begin
TheOncePost.ATimer.Enabled:=false;
TheOncePost.ATimer.Free;
end;
TheOncePost.Free;
end;
function TOncePost.PostIt: string;
var
f: textfile;
begin
if fileexists(GetTempDir + '.postit.tmp') then
begin
AssignFile(f, PChar(GetTempDir + '.postit.tmp'));
Reset(F);
Readln(F, TheMessage);
CloseFile(f);
DeleteFile(GetTempDir + '.postit.tmp');
Result := TheMessage;
end;
end;
{$IFDEF mse}
procedure TOncePost.onTimerPost(const sender: TObject);
{$else}
procedure TOncePost.onTimerPost(Sender: TObject);
{$endif}
begin
if PostIt <> '' then
if TheProc <> nil then
TheProc;
end;
{$IF DEFINED(LCL)}
procedure TOncePost.InitMessage(AOwner: TComponent);
begin
ATimer := TTimer.Create(AOwner); /// for lcl timer
ATimer.Enabled := false;
end;
{$endif}
{$IF DEFINED(fpgui)}
procedure TOncePost.InitMessage;
begin
ATimer := TfpgTimer.Create(1000); /// for fpGUI
ATimer.Enabled := false;
end;
{$endif}
{$IF DEFINED(mse)}
procedure TOncePost.InitMessage;
begin
ATimer := ttimer.Create(nil); /// for mse
ATimer.Enabled := false;
end;
{$endif}
function TOncePost.ExecProcess(const ACommandLine: string): string;
const
READ_BYTES = 2048;
var
VStrTemp: TStringList;
VMemoryStream: TMemoryStream;
VProcess: TProcess;
I64: longint;
VBytesRead: longint;
begin
VMemoryStream := TMemoryStream.Create;
VProcess := TProcess.Create(nil);
VStrTemp := TStringList.Create;
try
VBytesRead := 0;
{$WARN SYMBOL_DEPRECATED OFF}
VProcess.CommandLine := ACommandLine;
{$WARN SYMBOL_DEPRECATED ON}
VProcess.Options := [poUsePipes, poNoConsole];
VProcess.Execute;
while VProcess.Running do
begin
VMemoryStream.SetSize(VBytesRead + READ_BYTES);
I64 := VProcess.Output.Read((VMemoryStream.Memory + VBytesRead)^, READ_BYTES);
if I64 > 0 then
Inc(VBytesRead, I64)
else
Sleep(100);
end;
repeat
VMemoryStream.SetSize(VBytesRead + READ_BYTES);
I64 := VProcess.Output.Read((VMemoryStream.Memory + VBytesRead)^, READ_BYTES);
if I64 > 0 then
Inc(VBytesRead, I64);
until I64 <= 0;
VMemoryStream.SetSize(VBytesRead);
VStrTemp.LoadFromStream(VMemoryStream);
Result := Trim(VStrTemp.Text);
finally
VStrTemp.Free;
VProcess.Free;
VMemoryStream.Free;
end;
end;
procedure TOncePost.ListProcess(const AProcess: TStringList;
const AShowPID: boolean; const ASorted: boolean; const AIgnoreCurrent: boolean);
var
{$IFDEF UNIX}
I, J: integer;
VOldNameValueSeparator: char;
{$ENDIF}
{$IFDEF MSWINDOWS}
VSnapshotHandle: THandle;
VProcessEntry32: TProcessEntry32;
{$ENDIF}
begin
{$IFDEF UNIX}
VOldNameValueSeparator := AProcess.NameValueSeparator;
AProcess.NameValueSeparator := CLSUtilsProcessNameValueSeparator;
{$IFDEF FREEBSD}
AProcess.Text := ExecProcess('sh -c "ps -A | awk ''{ print $5 "=" $1 }''"');
{$ELSE}
AProcess.Text := ExecProcess('sh -c "ps -A | awk ''{ print $4 "=" $1 }''"');
{$ENDIF}
// debug
// writeln('Application list ');
// writeln('---------------------------------');
// writeln(AProcess.Text);
// writeln('---------------------------------');
J := AProcess.Count;
for I := AProcess.Count downto 1 do
begin
if (I > J - 3) or (AIgnoreCurrent and
(StrToIntDef(AProcess.ValueFromIndex[I - 1], -1) = integer(GetProcessID))) then
begin
AProcess.Delete(I - 1);
Continue;
end;
if not AShowPID then
AProcess.Strings[I - 1] := AProcess.Names[I - 1];
end;
AProcess.NameValueSeparator := VOldNameValueSeparator;
{$ENDIF}
{$IFDEF MSWINDOWS}
try
VSnapshotHandle := CreateToolHelp32SnapShot(TH32CS_SNAPALL, 0);
VProcessEntry32.dwSize := SizeOf(TProcessEntry32);
Process32First(VSnapshotHandle, VProcessEntry32);
repeat
if AIgnoreCurrent and (GetProcessID = VProcessEntry32.th32ProcessID) then
Continue;
if AShowPID then
AProcess.Add(VProcessEntry32.szExeFile + CLSUtilsProcessNameValueSeparator +
IntToStr(VProcessEntry32.th32ProcessID))
else
AProcess.Add(VProcessEntry32.szExeFile);
until (not Process32Next(VSnapshotHandle, VProcessEntry32));
except
end;
{$ENDIF}
if AProcess.Count > 0 then
AProcess.Delete(0);
AProcess.Sorted := ASorted;
end;
procedure TOncePost.RunOnce(AMessage: string);
var
VProcess: TStringList;
x, y: integer;
f: textfile;
begin
x := 0;
y := 0;
VProcess := TStringList.Create;
ListProcess(VProcess, False, False, False);
while x < VProcess.Count do
begin
if pos(ApplicationName, VProcess.Strings[x]) > 0 then
Inc(y);
if y > 1 then
begin
if AMessage <> '' then
begin
AssignFile(f, PChar(GetTempDir + '.postit.tmp'));
rewrite(f);
append(f);
writeln(f, AMessage);
Flush(f);
CloseFile(f);
end;
Halt;
end;
Inc(x);
end;
if ParamStr(1) <> '' then
begin
AssignFile(f, PChar(GetTempDir + '.postit.tmp'));
rewrite(f);
append(f);
writeln(f,AMessage);
Flush(f);
CloseFile(f);
end;
VProcess.Free;
end;
end.