2004. január 21., szerda

Queue up message forms in a TStringList


Problem/Question/Abstract:

I use a timer to check some conditions. If something special happens, I display a message form (using MyMessage.ShowModal, because I need an answer from the user). The timer goes on, so several of this Messages could be displayed simultaneously. What I want to do: Queue this messages and just display one. If the message is done, the next one is to be displayed.

Answer:

Simple idea: Instead of immediately popping up each message, queue them up into a stringlist. With a second timer (set to an appropriate interval) , process the message list, and delete/ take action. See example below (variations with enabling/ disabling timer2 are possible)

procedure TForm1.FormCreate(Sender: TObject);
begin
  MsgList := TStringList.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  MsgList.Free;
end;

{check for abnormal conditions}

procedure TForm1.Timer1Timer(Sender: TObject);
const
  msgNumber: integer = 0;
begin
  if Random > 0.5 then
  begin
    MsgList.Add('message ' + IntToStr(msgNumber) + ' @ ' + TimeToStr(Now));
    inc(MsgNumber);
  end;
end;

{process messages}

procedure TForm1.Timer2Timer(Sender: TObject);
begin
  Timer2.Enabled := false; {extremely important !}
  while MsgList.Count > 0 do
  begin
    {show oldest messages first}
    ShowMessage(MsgList[0] + ' viewed @' + TimeToStr(Now));
    MsgList.Delete(0);
    {your specific actions ...}
  end;
  Timer2.Enabled := true;
end;

Nincsenek megjegyzések:

Megjegyzés küldése