Author: Jonas Bilinkevicius
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)
1 procedure TForm1.FormCreate(Sender: TObject);
2 begin3 MsgList := TStringList.Create;
4 end;
5 6 procedure TForm1.FormDestroy(Sender: TObject);
7 begin8 MsgList.Free;
9 end;
10 11 {check for abnormal conditions}12 13 procedure TForm1.Timer1Timer(Sender: TObject);
14 const15 msgNumber: integer = 0;
16 begin17 if Random > 0.5 then18 begin19 MsgList.Add('message ' + IntToStr(msgNumber) + ' @ ' + TimeToStr(Now));
20 inc(MsgNumber);
21 end;
22 end;
23 24 {process messages}25 26 procedure TForm1.Timer2Timer(Sender: TObject);
27 begin28 Timer2.Enabled := false; {extremely important !}29 while MsgList.Count > 0 do30 begin31 {show oldest messages first}32 ShowMessage(MsgList[0] + ' viewed @' + TimeToStr(Now));
33 MsgList.Delete(0);
34 {your specific actions ...}35 end;
36 Timer2.Enabled := true;
37 end;