forked from vladk1973/npp.connections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadUnit.pas
123 lines (104 loc) · 2.82 KB
/
ThreadUnit.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
unit ThreadUnit;
interface
uses
Winapi.Windows, System.SysUtils, System.Classes, Vcl.ActnList;
type
TSimpleThread = class;
TThreadExecObject = class
private
FName: string;
FDescription: string;
FErrMessage: string;
FWinHandle: THandle;
FMsg: UINT;
FOnBeforeAction: TAction;
FOnAfterAction: TAction;
FUserObject: TPersistent;
FUserTree: TPersistent;
FUserTag: NativeInt;
procedure SendNotification;
protected
FThread: TSimpleThread;
procedure Execute; virtual; abstract;
public
constructor Create;
procedure Start;
procedure Finish;
property UserTree: TPersistent read FUserTree write FUserTree;
property UserObject: TPersistent read FUserObject write FUserObject;
property UserTag: NativeInt read FUserTag write FUserTag;
property ErrMessage: string read FErrMessage write FErrMessage;
property Name: string read FName write FName;
property WinHandle: THandle read FWinHandle write FWinHandle;
property Description: string read FDescription write FDescription;
property Msg: UINT read FMsg write FMsg;
property OnBeforeAction: TAction read FOnBeforeAction write FOnBeforeAction;
property OnAfterAction: TAction read FOnAfterAction write FOnAfterAction;
end;
TExecObject = TThreadExecObject;
TSimpleThread = class(TThread)
private
{ Private declarations }
FExecObject: TThreadExecObject;
protected
procedure Execute; override;
public
{ Public declarations }
property ExecObject: TThreadExecObject read FExecObject write FExecObject;
property Terminated;
end;
implementation
uses Winapi.ActiveX, ConstUnit;
{ TSimpleThread }
procedure TSimpleThread.Execute;
begin
Priority := tpLower;
CoInitialize(nil);
try
if not Terminated then
try
FExecObject.Execute;
except
on E: Exception do
FExecObject.ErrMessage := E.Message;
end;
finally
Terminate;
FExecObject.SendNotification;
CoUnInitialize;
end;
end;
{ TThreadExecObject }
constructor TThreadExecObject.Create;
begin
inherited;
FMsg := WM_USER_MESSAGE_FROM_THREAD;
end;
procedure TThreadExecObject.Finish;
begin
if Assigned(FOnAfterAction) then
begin
FOnAfterAction.Tag := NativeInt(Self);
FOnAfterAction.Execute;
end;
end;
procedure TThreadExecObject.SendNotification;
begin
SendMessage(FWinHandle,FMsg,0,LPARAM(Self));
end;
procedure TThreadExecObject.Start;
begin
if Assigned(FOnBeforeAction) then
begin
FOnBeforeAction.Tag := NativeInt(Self);
FOnBeforeAction.Execute;
end;
FThread := TSimpleThread.Create(True);
FThread.FreeOnTerminate := True;
with TSimpleThread(FThread) do
begin
ExecObject := Self;
FThread.Start;
end;
end;
end.