delphimvcframework/examples/QueueAck/Receiver/ThreadReceiver.pas

110 lines
2.4 KiB
ObjectPascal
Raw Normal View History

2012-12-27 23:32:04 +01:00
unit ThreadReceiver;
interface
uses
System.Classes,
StompClient,
StompTypes;
type
TThreadReceiver = class(TThread)
private
FStompClient: TStompClient;
FStompFrame: IStompFrame;
procedure SetStompClient(const Value: TStompClient);
protected
procedure Execute; override;
public
procedure ReceiveAlarmMemo;
procedure UpdateMessageMemo;
procedure UpdateMessageIdEdit;
constructor Create(CreateSuspended: Boolean); overload;
property StompClient: TStompClient read FStompClient write SetStompClient;
end;
implementation
2016-08-02 18:15:11 +02:00
{
2012-12-27 23:32:04 +01:00
Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
2016-08-02 18:15:11 +02:00
Synchronize(UpdateCaption);
2012-12-27 23:32:04 +01:00
and UpdateCaption could look like,
2016-08-02 18:15:11 +02:00
procedure TThreadReceiver.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
or
Synchronize(
procedure
begin
Form1.Caption := 'Updated in thread via an anonymous method'
end
)
);
2012-12-27 23:32:04 +01:00
where an anonymous method is passed.
2016-08-02 18:15:11 +02:00
Similarly, the developer can call the Queue method with similar parameters as
2012-12-27 23:32:04 +01:00
above, instead passing another TThread class as the first parameter, putting
the calling thread in a queue with the other thread.
2016-08-02 18:15:11 +02:00
2012-12-27 23:32:04 +01:00
}
2016-08-02 18:15:11 +02:00
uses ReceiverForm, System.SysUtils;
2012-12-27 23:32:04 +01:00
{ TThreadReceiver }
constructor TThreadReceiver.Create(CreateSuspended: Boolean);
begin
FStompFrame := TStompFrame.Create;
inherited Create(CreateSuspended);
end;
procedure TThreadReceiver.Execute;
begin
NameThreadForDebugging('ThreadReceiver');
while not Terminated do
begin
2016-08-02 18:15:11 +02:00
if FStompClient.Receive(FStompFrame, 2000) then
2012-12-27 23:32:04 +01:00
begin
Sleep(100);
Synchronize(ReceiveAlarmMemo);
Synchronize(UpdateMessageIdEdit);
end
else
begin
Synchronize(UpdateMessageMemo);
end;
end;
end;
procedure TThreadReceiver.ReceiveAlarmMemo;
begin
2016-08-02 18:15:11 +02:00
ReceiverMainForm.MessageMemo.Lines.Add(
StringReplace(FStompFrame.Output, #10, sLineBreak, [rfReplaceAll]));
2012-12-27 23:32:04 +01:00
end;
procedure TThreadReceiver.SetStompClient(const Value: TStompClient);
begin
FStompClient := Value;
end;
procedure TThreadReceiver.UpdateMessageIdEdit;
begin
ReceiverMainForm.MessageIdEdit.Text := FStompFrame.GetHeaders.Value('message-id');
end;
procedure TThreadReceiver.UpdateMessageMemo;
begin
2016-08-02 18:15:11 +02:00
//ReceiverMainForm.MessageMemo.Lines.Add('Wait Messages....');
2012-12-27 23:32:04 +01:00
end;
end.