Re: array with same value all the time?




Tom de Neef wrote:
"Sonnich" <sonnich.jensen@xxxxxxxxxxxxxx> schreef in bericht
news:1143474738.112955.64510@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi!

I have a project where I collect data into this:

TDataRec = record
Name: string[81];
RecordTime: TDateTime;
Value: double;
end;

I have an array ot that; where I add data when I get it (it happens in
a thread) - there is a TCristicalSection to avoid multiple instances
using it.
There is a procedure in the thread, which adds data.

The execute part looks at data, when there is enough, it goes in,
processes data, and delete them (that means - moving them, and setting
the array that much shorter using SetLenght) - usually it is cleared to
0 size.

{ delete used data }
i := iUsedData;
while i < Length(aData) do
begin
aData[i-iUsedData] := aData[i];
Inc(i);
end;
SetLength(aData, Length(aData) - iUsedData);

My problem is that sometimes for some reason, the Value is the same for
all records - that is possible, that it might happen once in a million
years, but not once a day as I get it. The name is the same, but the
timestamp is chaning correctly. It is just the recieved value, which
repeats itself.


That behaviour can not be the result of the code you publish above, which
just shifts data. I would say that it happens at the point where the data is
collected. You say consecutive records have Name and Value identical but the
timestamp changes. You do not show code of the data collection part.

The collecting is done like this:

procedure TSPCSaverThread.AddData(sName: string; Value: double;
dtDateTime: TDateTime);
var
i: integer;
begin
if not Assigned(csData) then
csData := TCriticalSection.Create;
try
csData.Acquire;

{ save data to array }
i := Length(aData);
SetLength(aData, i+1);
aData[i].Name := sName;
aData[i].RecordTime := dtDateTime;
aData[i].Value := Value;
finally
csData.Release;
end;
end;


This should not cuase any problems

I'll look deeper into it

.



Relevant Pages

  • array with same value all the time?
    ... TDataRec = record ... the array that much shorter using SetLenght) - usually it is cleared to ... SetLength- iUsedData); ...
    (comp.lang.pascal.delphi.misc)
  • Re: array with same value all the time?
    ... the array that much shorter using SetLenght) - usually it is cleared to ... timestamp changes. ... You do not show code of the data collection part. ... Is there a particular reason to use a dynamic array rather than a Tlist ...
    (comp.lang.pascal.delphi.misc)