unit Messagner.View;
interface
uses
System.SysUtils, System.Classes, System.Types,
System.Messaging, System.SyncObjs, System.Generics.Collections,
Vcl.Controls,
Vcl.Forms,
Vcl.StdCtrls;
type
TIdleMessage =
class(System.Messaging.TMessage)
end;
TDateTimeMessage =
class(System.Messaging.TMessage<TDateTime>)
end;
TForm1 =
class(TForm)
mmoLog : TMemo;
procedure FormCreate(Sender : TObject);
procedure FormDestroy(Sender : TObject);
private
FIdleID,
FDateTimeID : Integer;
FQueue : TQueue<TDateTime>;
procedure OnNewDateTimeMessage(
const Sender : TObject;
const M : TMessage);
procedure GetIdleMessage(
const Sender : TObject;
const M : TMessage);
procedure DoSendMessage(
const ADateTime : TDateTime);
procedure LogToMemo(
const Text :
string);
public
procedure DoIdle(Sender : TObject;
var Done : Boolean);
end;
var
Form1 : TForm1;
implementation
{$R *.dfm}
procedure TForm1.LogToMemo(
const Text :
string);
begin
mmoLog.Lines.Add(Text);
end;
procedure TForm1.OnNewDateTimeMessage(
const Sender : TObject;
const M : TMessage);
var
LMessage : TDateTimeMessage;
begin
LMessage := M
as TDateTimeMessage;
LogToMemo(sLineBreak + '
- - - > ' + FormatDateTime('
hh:mm:ss:zzz', LMessage.Value) + sLineBreak);
end;
procedure TForm1.DoIdle(Sender : TObject;
var Done : Boolean);
begin
TMessageManager.DefaultManager.SendMessage(Self, TIdleMessage.Create, False);
end;
procedure TForm1.FormCreate(Sender : TObject);
begin
Vcl.Forms.Application.OnIdle := DoIdle;
FQueue := TQueue<TDateTime>.Create;
FIdleID := TMessageManager.DefaultManager.SubscribeToMessage(TIdleMessage, GetIdleMessage);
FDateTimeID := TMessageManager.DefaultManager.SubscribeToMessage(TDateTimeMessage, OnNewDateTimeMessage);
end;
procedure TForm1.FormDestroy(Sender : TObject);
begin
TMessageManager.DefaultManager.Unsubscribe(TIdleMessage,FIdleID,true);
TMessageManager.DefaultManager.Unsubscribe(TDateTimeMessage,FDateTimeID,true);
end;
procedure TForm1.DoSendMessage(
const ADateTime : TDateTime);
var
LMessage : TDateTimeMessage;
begin
LMessage := TDateTimeMessage.Create(ADateTime);
TMessageManager.DefaultManager.SendMessage(Self, LMessage, True);
end;
procedure TForm1.GetIdleMessage(
const Sender : TObject;
const M : TMessage);
var
LDateTime : TDateTime;
begin
LDateTime := System.SysUtils.Now;
if FQueue.Count <> 0
then
begin
if FQueue.Peek <> LDateTime
then
begin
FQueue.Enqueue(LDateTime);
end;
end
else
begin
FQueue.Enqueue(LDateTime);
end;
end;
end.