-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.pas
104 lines (84 loc) · 2.41 KB
/
MainForm.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
unit MainForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,
StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
procedure TaskComplete(Sender: TObject);
procedure HandleCheckpoint1(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
TSpecificTaskThread = Class(TThread)
private
FInterimResult: string;
FFinalResult: string;
FOnCheckpoint1: TNotifyEvent;
protected
procedure DoCheckpoint1;
procedure Execute; override;
public
property InterimResult: string read FInterimResult;
property FinalResult: string read FFinalResult;
property OnCheckpoint1 : TNotifyEvent read FOnCheckpoint1 write FOnCheckpoint1;
End;
procedure TSpecificTaskThread.DoCheckpoint1;
begin
if Assigned(FOnCheckpoint1) then FOnCheckpoint1(Self);
end;
procedure TSpecificTaskThread.Execute;
var
aGuid: TGuid;
begin
FInterimResult := '';
FFinalResult := '';
// call an external API (web, Hydra etc here)
Sleep(1500);
CreateGuid(aGuid);
FInterimResult := GuidToString(aGuid);
Synchronize(DoCheckpoint1);
// call an external API (web, Hydra etc here)
Sleep(1500);
CreateGuid(aGuid);
FFinalResult := GuidToString(aGuid);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
oThread : TSpecificTaskThread;
begin
// True tells the thread not to start until we've configured it
oThread := TSpecificTaskThread.Create(True);
// OnCheckpoint1 is called on the UI thread at an intermediate progress event
oThread.OnCheckpoint1 := HandleCheckpoint1;
// OnTerminate is called on the UI thread after Execute completes
oThread.OnTerminate := TaskComplete;
// don't need this if we hang onto oThread and free it
oThread.FreeOnTerminate := True;
oThread.Resume;
Memo1.Lines.Add(Format('%s - thread started', [DateTimeToStr(Now)]));
end;
procedure TForm1.HandleCheckpoint1(Sender: TObject);
var
value: string;
begin
value := TSpecificTaskThread(Sender).InterimResult;
Memo1.Lines.Add(Format('%s - checkpoint, result ''%s''', [DateTimeToStr(Now), value]));
end;
procedure TForm1.TaskComplete(Sender: TObject);
var
value: string;
begin
value := TSpecificTaskThread(Sender).FinalResult;
Memo1.Lines.Add(Format('%s - thread finished, result ''%s''', [DateTimeToStr(Now), value]));
end;
end.