-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathBassPlayer.LoadHandle.pas
More file actions
285 lines (255 loc) · 5.84 KB
/
BassPlayer.LoadHandle.pas
File metadata and controls
285 lines (255 loc) · 5.84 KB
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
unit BassPlayer.LoadHandle;
interface
uses
System.SysUtils, System.Variants, System.Classes, System.Generics.Collections,
System.Threading;
type
TLoadThread = class
class var
TaskPoll: TThreadPool;
private
FBefore: TProc;
FAsync: TFunc<TLoadThread, Boolean>;
FAfter: TProc<Boolean>;
FDoStopThread: Boolean;
FWorking: Boolean;
protected
procedure FProc;
public
procedure Execute;
procedure Stop; virtual;
procedure Await(AppProc: Boolean = True);
property IsWorking: Boolean read FWorking;
property NeedStop: Boolean read FDoStopThread;
constructor Create(Before: TProc; Async: TFunc<TLoadThread, Boolean>; After: TProc<Boolean>);
class procedure StopAll;
end;
TLoadPlaylist = class(TLoadThread)
private
FAsync: TFunc<TLoadThread, Integer, Integer, Boolean>;
procedure FProc(OwnerId, Id: Integer);
public
procedure Execute(OwnerId, Id: Integer);
constructor Create(Before: TProc; Async: TFunc<TLoadThread, Integer, Integer, Boolean>; After: TProc<Boolean>);
end;
TLoadSearch = class(TLoadThread)
private
FAsync: TFunc<TLoadThread, string, Boolean>;
procedure FProc(Query: string);
public
procedure Execute(Query: string);
constructor Create(Before: TProc; Async: TFunc<TLoadThread, string, Boolean>; After: TProc<Boolean>);
end;
TLoaders = class(TList<TLoadThread>)
procedure Stop;
procedure Await;
procedure Clear;
end;
implementation
procedure TLoadThread.Await;
begin {
while FWorking do
if AppProc then
Application.ProcessMessages; }
end;
constructor TLoadThread.Create(Before: TProc; Async: TFunc<TLoadThread, Boolean>; After: TProc<Boolean>);
begin
FBefore := Before;
FAsync := Async;
FAfter := After;
FWorking := False;
FDoStopThread := False;
end;
procedure TLoadThread.Execute;
begin
FProc;
end;
procedure TLoadThread.FProc;
begin
TTask.Run(
procedure
var
DoAfter: Boolean;
begin
FDoStopThread := True;
while FWorking do
Sleep(100);
FDoStopThread := False;
FWorking := True;
TThread.Synchronize(TThread.Current,
procedure
begin
if Assigned(FBefore) then
FBefore;
end);
try
try
DoAfter := FAsync(Self);
TThread.Synchronize(TThread.Current,
procedure
begin
if Assigned(FAfter) then
begin
FAfter(DoAfter and (not FDoStopThread));
end;
end);
finally
FWorking := False;
end;
except
end;
end, TaskPoll);
end;
procedure TLoadThread.Stop;
begin
FDoStopThread := True;
end;
class procedure TLoadThread.StopAll;
begin
if Assigned(TaskPoll) then
TaskPoll.Free;
TaskPoll := TThreadPool.Create;
end;
{ TLoadPlaylist }
constructor TLoadPlaylist.Create(Before: TProc; Async: TFunc<TLoadThread, Integer, Integer, Boolean>; After: TProc<Boolean>);
begin
FBefore := Before;
FAsync := Async;
FAfter := After;
FWorking := False;
FDoStopThread := False;
end;
procedure TLoadPlaylist.Execute(OwnerId, Id: Integer);
begin
FProc(OwnerId, Id);
end;
procedure TLoadPlaylist.FProc(OwnerId, Id: Integer);
begin
TTask.Run(
procedure
var
DoAfter: Boolean;
begin
FDoStopThread := True;
while FWorking do
Sleep(100);
FDoStopThread := False;
FWorking := True;
TThread.Synchronize(TThread.Current,
procedure
begin
if Assigned(FBefore) then
FBefore;
end);
try
try
DoAfter := FAsync(Self, OwnerId, Id);
TThread.Synchronize(TThread.Current,
procedure
begin
if Assigned(FAfter) then
begin
FAfter(DoAfter and (not FDoStopThread));
end;
end);
finally
FWorking := False;
end;
except
end;
end, TaskPoll);
end;
{ TLoaders }
procedure TLoaders.Await;
var
i: Integer;
FComplete: Boolean;
begin
repeat
FComplete := True;
for i := 0 to Count - 1 do
begin
if Items[i].IsWorking then
FComplete := False;
end; {
if not FComplete then
Application.ProcessMessages; }
until FComplete;
end;
procedure TLoaders.Clear;
{$IFNDEF AUTOREFCOUNT}
var
i: Integer;
{$ENDIF}
begin
{$IFNDEF AUTOREFCOUNT}
for i := 0 to Count - 1 do
Items[i].Free;
{$ENDIF}
inherited;
end;
procedure TLoaders.Stop;
var
i: Integer;
begin
for i := 0 to Count - 1 do
Items[i].Stop;
end;
{ TLoadSearch }
constructor TLoadSearch.Create(Before: TProc; Async: TFunc<TLoadThread, string, Boolean>; After: TProc<Boolean>);
begin
FBefore := Before;
FAsync := Async;
FAfter := After;
FWorking := False;
FDoStopThread := False;
end;
procedure TLoadSearch.Execute(Query: string);
begin
FProc(Query);
end;
procedure TLoadSearch.FProc(Query: string);
begin
TTask.Run(
procedure
var
DoAfter: Boolean;
begin
FDoStopThread := True;
while FWorking do
Sleep(100);
FDoStopThread := False;
FWorking := True;
TThread.Synchronize(TThread.Current,
procedure
begin
if Assigned(FBefore) then
FBefore;
end);
try
try
DoAfter := FAsync(Self, Query);
TThread.Synchronize(TThread.Current,
procedure
begin
if Assigned(FAfter) then
begin
FAfter(DoAfter and (not FDoStopThread));
end;
end);
finally
FWorking := False;
end;
except
end;
end, TaskPoll);
end;
initialization
TLoadThread.TaskPoll := TThreadPool.Create;
finalization
if Assigned(TLoadThread.TaskPoll) then
begin
TLoadThread.TaskPoll.Free;
TLoadThread.TaskPoll := nil;
end;
end.