-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathAnonCommand.pas
716 lines (596 loc) · 19.2 KB
/
AnonCommand.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
unit AnonCommand;
interface
{x$DEFINE ANON_SYNC_EXEC}
uses
betterobject, debug, System.Classes, System.SysUtils, System.Generics.Collections, better_collections, typex, systemx, managedthread, commandprocessor, numbers, backgroundcommandprocessor;
type
EAnonymousThreadException = class(Exception);
Tcmd_Test = class(TCommand)
public
procedure DoExecute;override;
end;
TAnonymousCommand<T> = class(TCommand)
private
Err: Exception;
FThreadFunc: TFunc<T>;
FOnErrorProc: TProc<Exception>;
FOnFinishedProc: TProc<T>;
FResult: T;
FStartSuspended: Boolean;
FSynchronizeFinish: boolean;
FSynchronizeExecute: boolean;
strict private
procedure SyncFinished;
procedure SyncExecute;
procedure SyncError;
protected
procedure DoExecute; override;
public
vResultEx: variant;
parenthold: IUnknown;
property OnErrorProc: TProc<Exception> read FonErrorProc;
procedure InitExpense; override;
procedure Detach; override;
property SynchronizeFinish: boolean read FSynchronizeFinish write FSynchronizeFinish;
property SynchronizeExecute: boolean read FSynchronizeExecute write FSynchronizeExecute;
constructor CreateAndTrackX(tracker: TCommandList<TCommand>; AThreadFunc: TFunc<T>; AOnFinishedProc: TProc<T>;AOnErrorProc: TProc<Exception>; ACreateSuspended: Boolean = False);virtual;
constructor Create(AThreadFunc: TFunc<T>; AOnFinishedProc: TProc<T>;AOnErrorProc: TProc<Exception>; ACreateSuspended: Boolean = False;
FreeOnComplete: Boolean = True);reintroduce;
destructor Destroy;override;
// class constructor Create;
// class destructor Destroy;
end;
TAnonymousGUICommand<T> = class(TAnonymousCommand<T>)
//THIS is an ANONYMOUs COMMAND that uses SYNCHRONIZE in OnFinish
//by default. You can choose to synchronize Execution and Finish of any anonymous
//command. This derivative simply has a different default for SynchronizeFinish
public
constructor Create(AThreadFunc: TFunc<T>; AOnFinishedProc: TProc<T>;AOnErrorProc: TProc<Exception>; ACreateSuspended: Boolean = False;
FreeOnComplete: Boolean = True);reintroduce;
end;
TAnonymousIteratorCommand = class(TCommand)
protected
procedure DoExecute; override;
public
iteration: int64;
batchcount: int64;
proc: TProc<int64>;
end;
TAnonymousFunctionCommand = class(TAnonymousCommand<boolean>)
public
constructor CreateInline(AThreadFunc: TProc; ACreateSuspended: Boolean = False;
FreeOnComplete: Boolean = false);reintroduce;
constructor CreateInlineWithGui(AThreadFunc, GuiFunc: TProc; ACreateSuspended: Boolean = False; FreeOnComplete: Boolean = false);reintroduce;
constructor CreateInlineWithGuiEx(AThreadFunc, GuiFunc: TProc; exProc: TProc<string>; ACreateSuspended: Boolean = False; FreeOnComplete: Boolean = false);reintroduce;
end;
TAnonTimerProc = reference to procedure();
TAnonymousTimer = class(TAnonymousCommand<boolean>)
public
timerproc: TAnonTimerProc;
procedure InitExpense;override;
end;
TPromise = record
private
cmd: IHolder<TAnonymousCommand<boolean>>;
public
parent: ^TPromise;
function thenDo(p: TProc): TPromise;
function thenGUI(p: TProc): TPromise;
function Go: TPromise;
function WaitFor(iupTo: int64 = -1): boolean;
end;
function Promise(proc: TProc): TPromise;
function InlineProc(proc: TProc; bStart: boolean = true): TAnonymousCommand<boolean>;
function InlineIteratorProc(idx: ni; proc: TProc<int64>): TAnonymousIteratorCommand;
function InlineIteratorProcNS(idx: ni; proc: TProc<int64>): TAnonymousIteratorCommand;
function InlineIteratorGroupProc(idx: ni; batchcount: ni; proc: TProc<int64>): TAnonymousIteratorCommand;
function InlineProcWithGui(proc, guiproc: TProc): TAnonymousCommand<boolean>;
function InlineProcWithGuiEx(proc, guiproc: TProc; exProc:TProc<string>): TAnonymousCommand<boolean>;
function InlineProcWithGuiExUnstarted(proc, guiproc: TProc; exProc:TProc<string>): TAnonymousCommand<boolean>;
procedure ForXFake(iStart, iEnd_Ignored, iMinBatchSize,iMaxBatchSize: int64; doproc: TProc<int64>;opts: TForXOptions = []; prog: TProc<TProgress> = nil; cp: TCommandProcessor = nil);overload;
procedure ForXFake(iStart, iEnd_Ignored, iMinBatchSize: int64; doproc: TProc<int64>;opts: TForXOptions = []; prog: TProc<TProgress> = nil; cp: TCommandProcessor = nil);overload;
procedure ForX(iStart, iEnd_Ignored, iMinBatchSize: int64; doproc: TProc<int64>;opts: TForXOptions = []; prog: TProc<TProgress> = nil; cp: TCommandProcessor = nil);overload;
procedure ForX(iStart, iEnd_Ignored, iMinBatchSize,iMaxBatchSize: int64; doproc: TProc<int64>;opts: TForXOptions = []; prog: TProc<TProgress> = nil; cp: TCommandProcessor = nil);overload;
procedure ForX_NoWait(iStart, iEnd, iMinBatchSize: int64; doproc: TProc<int64>; cp: TCommandProcessor = nil);
//function InlineProc<T>(proc: TProc): TAnonymousCommand<T,boolean>;
function SetTimer(interval: ni; ontimerproc: TAnonTimerProc): TAnonymousTimer;
function SetTimerGUI(interval: ni; ontimerproc: TAnonTimerProc): TAnonymousTimer;
implementation
{$IFDEF MACOS}
uses
{$IFDEF IOS}
iOSapi.Foundation
{$ELSE}
MacApi.Foundation
{$ENDIF IOS}
;
{$ENDIF MACOS}
procedure ForXFake(iStart, iEnd_Ignored, iMinBatchSize,iMaxBatchSize: int64; doproc: TProc<int64>;opts: TForXOptions = []; prog: TProc<TProgress> = nil; cp: TCommandProcessor = nil);overload;
begin
ForXFake(iStart, iEnd_Ignored, iMinBatchSize, doproc, opts, prog, cp);
end;
procedure ForXFake(iStart, iEnd_Ignored, iMinBatchSize: int64; doproc: TProc<int64>;opts: TForXOptions = []; prog: TProc<TProgress> = nil; cp: TCommandProcessor = nil);
var
p: TProgress;
begin
var realend := iEnd_Ignored-1;
if TForXoption.fxEndInclusive in opts then
realend := realend + 1;
for var x := iStart to realend do begin
if assigned(prog) then begin
p.step := x;
p.stepcount := realend;
prog(p);
end;
doProc(x);
end;
end;
procedure ForX(iStart, iEnd_Ignored, iMinBatchSize: int64;
doproc: TProc<int64>;opts: TForXOptions = []; prog: TProc<TProgress> = nil; cp: TCommandProcessor = nil);
begin
// if IsDebuggerAttached then opts := opts + [fxLimit4Threads];
ForX(iStart,iEnd_Ignored,iMinBatchSize, 0, doproc, opts, prog,cp);
end;
procedure ForX(iStart, iEnd_Ignored, iMinBatchSize, iMaxBatchSize: int64;
doproc: TProc<int64>;opts: TForXOptions = []; prog: TProc<TProgress> = nil; cp: TCommandProcessor = nil);
begin
if fxEndInclusive in opts then begin
if iEnd_Ignored < iStart then
exit;
end else begin
if iEnd_Ignored <= iStart then
exit;
end;
// if IsDebuggerAttached then opts := opts + [fxLimit4Threads];
if iMaxBatchSize = 0 then
iMaxBatchSize := SIMPLE_BATCH_SIZE;
var cl := TCommandList<TAnonymousIteratorCommand>.create;
try
var cpus := GetEnabledCPUCount;
var t := iStart;
if (iEnd_Ignored >= iStart) then begin
var totalsz := iEnd_Ignored-iStart;
if fxNoCPUExpense in opts then
cpus := greaterof(1,totalsz);
if fxLimit1Thread in opts then
cpus := 1;
if fxLimit2Threads in opts then
cpus := 2;
if fxLimit4Threads in opts then
cpus := 4;
if fxLimit8Threads in opts then
cpus := 8;
if fxLimit16Threads in opts then
cpus := 16;
if fxLimit32Threads in opts then
cpus := 32;
if fxLimit64Threads in opts then
cpus := 64;
if fxLimit256Threads in opts then
cpus := 256;
if fxLimit1024Threads in opts then
cpus := 1024;
if fxEndInclusive in opts then
inc(totalsz);
var cx := totalsz;
while cx > 0 do begin
var thissz := lesserof(cx, greaterof(iMinBatchSize, (totalsz div cpus)));
thissz := lesserof(iMaxBatchSize, thissz);
var c := TAnonymousIteratorCommand.Create;
c.iteration := t;
c.batchcount := thissz;
c.proc := doProc;
cl.add(c);
if fxNoCPUExpense in opts then
c.CPUExpense := 0.0;
if fxLimit1Thread in opts then
c.memoryexpense := 1/2;
if fxLimit2Threads in opts then
c.memoryexpense := 1/2;
if fxLimit4Threads in opts then
c.memoryexpense := 1/4;
if fxLimit8Threads in opts then
c.memoryexpense := 1/8;
if fxLimit16Threads in opts then
c.memoryexpense := 1/16;
if fxLimit32Threads in opts then
c.memoryexpense := 1/32;
if fxLimit64Threads in opts then
c.memoryexpense := 1/64;
if fxLimit256Threads in opts then
c.memoryexpense := 1/256;
if fxLimit1024Threads in opts then
c.memoryexpense := 1/1024;
c.RaiseExceptions := fxRaiseExceptions in opts;
c.FireForget := false;
if cp = nil then
c.start(ForXCmd)
else
c.start(cp);
dec(cx, thissz);
inc(t, thissz);
end;
end else begin
debug.Log('warning, not implemented when end < start');
exit;
end;
cl.WaitForAll_DestroyWhileWaiting(prog);
if assigned(prog) then begin
var p: TProgress;
p.step := -1;
p.stepcount := 0;
prog(p);
end;
finally
cl.free;
end;
end;
procedure ForX_NoWait(iStart, iEnd, iMinBatchSize: int64; doproc: TProc<int64>; cp: TCommandProcessor = nil);
begin
// var cl := TCommandList<TAnonymousIteratorCommand>.create;
try
var cpus := GetEnabledCPUCount;
var t := iStart;
if (iEnd >= iStart) then begin
var totalsz := iEnd-iStart;
var cx := totalsz;
while cx > 0 do begin
var thissz := lesserof(cx, greaterof(iMinBatchSize, (totalsz div cpus)));
var c := TAnonymousIteratorCommand.Create;
c.iteration := t;
c.batchcount := thissz;
c.proc := doProc;
// cl.add(c);
c.FireForget := true;
if cp = nil then
c.start(ForXCmd)
else
c.start(cp);
dec(cx, thissz);
inc(t, thissz);
end;
end else begin
raise ECritical.create('not implemented when end < start');
end;
// cl.WaitForAll_DestroyWhileWaiting;
finally
// cl.free;
end;
end;
{ TAnonymousCommand }
//class constructor TAnonymousCommand<T>.Create;
//begin
// inherited;
//end;
//class destructor TAnonymousCommand<T>.Destroy;
//begin
// inherited;
//end;
procedure TAnonymousCommand<T>.Detach;
begin
// Debug.log(self, 'Detaching');
if detached then exit;
inherited;
end;
function Promise(proc: TProc): TPromise;
begin
result.cmd := THolder<TAnonymousCommand<boolean>>.create(TAnonymousFunctionCommand.createinline(proc,true,false));
result.cmd.o.startondestroy := true;
result.parent := nil;
end;
function InlineProc(proc: TProc; bStart: boolean = true): TAnonymousCommand<boolean>;
var
res: TAnonymousCommand<boolean>;
begin
res := TAnonymousFunctionCommand.createinline(proc, not bStart, false);
res.SynchronizeFinish := false;
result := res;
end;
function InlineIteratorProc(idx: ni; proc: TProc<int64>): TAnonymousIteratorCommand;
begin
result := InlineIteratorProcNS(idx, proc);
result.start;
end;
function InlineIteratorProcNS(idx: ni; proc: TProc<int64>): TAnonymousIteratorCommand;
begin
result := TAnonymousIteratorCommand.create;
result.iteration := idx;
result.BATCHcount := 1;
result.proc := proc;
// result.CPUExpense := 1.0;
end;
function InlineIteratorGroupProc(idx: ni; batchcount: ni; proc: TProc<int64>): TAnonymousIteratorCommand;
begin
result := TAnonymousIteratorCommand.create;
result.iteration := idx;
result.batchcount := batchcount;
result.proc := proc;
// result.CPUExpense := 1.0;
result.start;
end;
function InlineProcWithGui(proc, guiproc: TProc): TAnonymousCommand<boolean>;
var
res: TAnonymousCommand<boolean>;
begin
res := TAnonymousFunctionCommand.createinlinewithgui(proc, guiproc, false, false);
result := res;
result.start;
end;
function InlineProcWithGuiEx(proc, guiproc: TProc; exProc:TProc<string>): TAnonymousCommand<boolean>;
var
res: TAnonymousCommand<boolean>;
begin
res := TAnonymousFunctionCommand.createinlinewithguiex(proc, guiproc, exProc, false, false);
result := res;
result.start;
end;
function InlineProcWithGuiExUnstarted(proc, guiproc: TProc; exProc:TProc<string>): TAnonymousCommand<boolean>;
var
res: TAnonymousCommand<boolean>;
begin
res := TAnonymousFunctionCommand.createinlinewithguiex(proc, guiproc, exProc, false, false);
result := res;
// result.start;
end;
constructor TAnonymousCommand<T>.Create(AThreadFunc: TFunc<T>; AOnFinishedProc: TProc<T>;
AOnErrorProc: TProc<Exception>; ACreateSuspended: Boolean = False; FreeOnComplete: Boolean = True);
begin
// Debug.Log('Creating '+self.GetObjectDebug);
FOnFinishedProc := AOnFinishedProc;
FOnErrorProc := AOnErrorProc;
FThreadFunc := AThreadFunc;
FireForget := FreeOnComplete;
FStartSuspended := ACreateSuspended;
FSynchronizeFinish := true;
{$IFDEF ANON_SYNC_EXEC}
FSynchronizeExecute := true;
{$ENDIF}
inherited Create();
if not ACreateSuspended then
Start;
end;
constructor TAnonymousCommand<T>.CreateAndTrackX(tracker: TCommandList<TCommand>;
AThreadFunc: TFunc<T>; AOnFinishedProc: TProc<T>;
AOnErrorProc: TProc<Exception>; ACreateSuspended: Boolean);
begin
Create(AthreadFunc, AonFinishedProc, AOnErrorProc, ACreateSuspended, false);
tracker.Add(self);
end;
destructor TAnonymousCommand<T>.Destroy;
begin
// Debug.Log('Destroying Inherited '+self.GetObjectDebug);
inherited;
// Debug.Log('Destroying '+self.GetObjectDebug);
end;
procedure TAnonymousCommand<T>.DoExecute;
{$IFDEF MACOS}
var
lPool: NSAutoreleasePool;
{$ENDIF}
begin
inherited;
// Debug.Log(self, 'Executing '+self.GetObjectDebug);
{$IFDEF MACOS}
//Need to create an autorelease pool, otherwise any autorelease objects
//may leak.
//See https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html#//apple_ref/doc/uid/20000047-CJBFBEDI
lPool := TNSAutoreleasePool.Create;
try
{$ENDIF}
try
if assigned(FthreadFunc) then begin
if FSynchronizeExecute then begin
TThread.Synchronize(self.Thread.realthread, SyncExecute)
end else
FResult := FThreadFunc;
end;
if assigned(FonFinishedProc) then begin
try
if FSynchronizeFinish and assigned(FOnFinishedProc) then
TThread.Synchronize(self.Thread.realthread, SyncFinished)
else
FOnFinishedProc(FResult);
except
on E:Exception do begin
Debug.Log('Exception during synchronized anon finish: '+e.message);
end;
end;
end;
except
on E: Exception do begin
Err := e;
ErrorMessage := e.message;
Error := true;
{$IFNDEF CONSOLE}
if FSynchronizeFinish then
TThread.Synchronize(self.Thread.realthread, SyncError)
else
{$ENDIF}
if assigned(FOnErrorProc) then
FOnErrorProc(E);
end;
end;
{$IFDEF MACOS}
finally
lPool.drain;
end;
{$ENDIF}
end;
procedure TAnonymousCommand<T>.InitExpense;
begin
inherited;
// self.Resources.SetResourceUsage('CTO_Anonymous', 1.0);
cpuExpense := 0.0;
end;
procedure TAnonymousCommand<T>.SyncError;
begin
FOnErrorProc(Err);
end;
procedure TAnonymousCommand<T>.SyncExecute;
begin
FResult := FThreadFunc;
end;
procedure TAnonymousCommand<T>.SyncFinished;
begin
FOnFinishedProc(fResult);
end;
{ Tcmd_Test }
procedure Tcmd_Test.DoExecute;
begin
inherited;
sleep(4000);
end;
procedure TAnonymousTimer.InitExpense;
begin
inherited;
CPuExpense := 0;
end;
function SetTimerGUI(interval: ni; ontimerproc: TAnonTimerProc): TAnonymousTimer;
begin
result := TAnonymousTimer.create(
function : boolean
begin
sleep(interval);
exit(true);
end,
procedure (b: boolean)
begin
ontimerproc();
end,
procedure (e: exception)
begin
end
);
result.SynchronizeFinish := true;
result.FireForget := true;
result.start;
end;
function SetTimer(interval: ni; ontimerproc: TAnonTimerProc): TAnonymousTimer;
begin
result := TAnonymousTimer.create(
function : boolean
begin
sleep(interval);
exit(true);
end,
procedure (b: boolean)
begin
ontimerproc();
end,
procedure (e: exception)
begin
end
);
result.SynchronizeFinish := false;
result.FireForget := true;
result.start;
end;
{ TAnonymousGUICommand<T> }
constructor TAnonymousGUICommand<T>.Create(AThreadFunc: TFunc<T>;
AOnFinishedProc: TProc<T>; AOnErrorProc: TProc<Exception>; ACreateSuspended,
FreeOnComplete: Boolean);
begin
inherited;
SynchronizeFinish := true;
end;
{ TAnonymousFunctionCommand }
constructor TAnonymousFunctionCommand.CreateInline(AThreadFunc: TProc;
ACreateSuspended, FreeOnComplete: Boolean);
var
funct: TFunc<boolean>;
begin
funct:= function (): boolean
begin
AthreadFunc();
result := true;
end;
Create(funct, nil, nil, ACreateSuspended, FreeOnComplete);
end;
constructor TAnonymousFunctionCommand.CreateInlineWithGui(AThreadFunc, GuiFunc: TProc;
ACreateSuspended, FreeOnComplete: Boolean);
var
func1: TFunc<boolean>;
func2: TProc<boolean>;
begin
func1:= function (): boolean
begin
if assigned(AThreadFunc) then
AthreadFunc();
result := true;
end;
func2:= procedure (b: boolean)
begin
if assigned(GuiFunc) then
GuiFunc();
end;
Create(func1, func2, procedure (e: exception) begin end, true, false);
SynchronizeFinish := true;
Start;
end;
constructor TAnonymousFunctionCommand.CreateInlineWithGuiEx(AThreadFunc,
GuiFunc: TProc; exProc: TProc<string>; ACreateSuspended,
FreeOnComplete: Boolean);
var
func1: TFunc<boolean>;
func2: TProc<boolean>;
func3: TProc<string>;
begin
func1:= function (): boolean
begin
AthreadFunc();
result := true;
end;
func2 := nil;
if assigned(guifunc) then
func2:= procedure (b: boolean)
begin
GuiFunc();
end;
func3 := nil;
if assigned(exproc) then
func3 := procedure (s: string)
begin
ExProc(s);
end;
Create(func1, func2, procedure (e: exception) begin func3(e.message) end, true, false);
SynchronizeFinish := true;
Start;
end;
{ TAnonymousIteratorCommand }
procedure TAnonymousIteratorCommand.DoExecute;
begin
inherited;
if batchcount = 0 then
batchcount := 1;
for var t:= 0 to batchcount-1 do begin
proc(iteration+t);
end;
end;
{ TPromise }
function TPromise.Go: TPromise;
begin
result := self;
cmd.o.StartChain;
end;
function TPromise.thenDo(p: TProc): TPromise;
begin
result := Promise(p);
result.parent := @self;
result.cmd.o.FireForget := false;
result.cmd.o.parenthold := result.cmd;
result.cmd.o.AddDependency(result.parent.cmd.o);
end;
function TPromise.thenGUI(p: TProc): TPromise;
begin
result := Promise(p);
result.parent := @self;
result.cmd.o.FireForget := false;
result.cmd.o.AddDependency(result.parent.cmd.o);
result.cmd.o.parenthold := result.cmd;
result.cmd.o.SynchronizeExecute := true;
end;
function TPromise.WaitFor(iupTo: int64): boolean;
begin
result := self.cmd.o.WaitFor(iUpTo);
end;
end.