I am writing an application that needs an accurate and continous clock 'pulse' every 40mS or less: basically it's sort of like a MIDI music sequencer, but for another purpose. Everything works fine - I've done a custom component that fires off a NotifyEvent at nice, regular intervals, and I can display that as a time. Here's the beef of my timer component:
procedure TTimecode.RunClock;
var
Interval : single;
fq, ct : int64;
MIDICount : integer;
begin
Interval := FRameRate / 1000;
MIDICount := 0;
while Enabled do //Keep looping
begin
QueryPerformanceFrequency(fq); //Get the frequency
Repeat
QueryPerformanceCounter(ct); //Get the current count
until (ct - OldCounter) / fq >= Interval; //Time in miliseconds since last loop
OldCounter := ct; //Keep this for next time around
If assigned(OnMIDIFrame) then OnMIDIFrame(self);
inc(MIDICount);
If MIDICount = 5 then MIDICount := 1;
If MIDICount = 1 then
If assigned(OnTCFrame) then OnTCFrame(self);
sleep(1); //Reduces processor load
end;
end;
In my main app code, this is used as follows:
procedure TfrmMain.MasterClockTCFrame(Sender: TObject); //Called once every full frame
var
n : integer;
begin
//Update clocks for any timelines running on internal sync
For n := 0 to length(TL) - 1 do
If TL[n].Sync = syInt then TL[n].Time := TL[n].Time + mspf;
frmTimeline.UpdateClock; //Update the timeline display
end;
TL is a dynamically created array of records, with Time as a field. UpdateClock is a little procedure that simply shows the time in an edit control. mspf is the time to increment in miliseconds for each event (each item in the TL array can have a different time).
Now here's the problem: if I click on any child window's caption bar, the clock stops. I don't mean the form's paint does not update, I mean it's like the whole thread stops, until I release the mouse. TL[x].Time is not updated until I release the mouse. My app is time critical, so this is clearly not good! I tried setting my apps thread priority to REALTIME_PRIORITY_CLASS, but that did not help. Any ideas? I need the clock to always keep counting, even if the user moves a window around.