List<object>.Add Override entire list on last record

  • Thread starter Thread starter labjac
  • Start date Start date
L

labjac

Guest
Hallo

Hope sombody can assist maybe I've been starting at this for to long, I got a list of data that is being populated with data recieved from a event, this is store in a event everytime the unit got data, I then monitor the dataRecieve flag and add the data to a list of data, but for some reason the last record will override all the previous records in the list.

Last block is the output from the Console, I write out all the values in the list througout the loop and all are well, till the List<objects> is done getting information, the it overrides all the previous records as can see from the Output snippet.

public class GetMeasurementController
{
public delegate void DataRecieved(object source, ScannerReturnObjectModel singleScanModel);
public event DataRecieved DataReceived;

public delegate void ScanningMessaging(string message, int ProgressCounter);
public event ScanningMessaging ScanningProgressMessages;

public delegate void ScanningCompleted(List<ScannerReturnObjectModel> ScanningData);
public event ScanningCompleted ScanComplete;

OnMeasurementDataRecieveHandler _dataEventHandler;

int _jobMaxTime;

bool dataRecievedFlag = false;
bool cycleTimeOut = false;
//bool dataTimeOut = false;

List<ScannerReturnObjectModel> _returnDataModelList;
ScannerReturnObjectModel _singleScannerReturnModel;

public GetMeasurementController()
{
_dataEventHandler = new OnMeasurementDataRecieveHandler();
_returnDataModelList = new List<ScannerReturnObjectModel>();
_returnDataModelList.Clear();
}

public List<ScannerReturnObjectModel> GetMeasurements(uint SensorID, int JobTimeOut)
{
_jobMaxTime = JobTimeOut;
int dataCounter = 0;
var cycleTimer = new System.Timers.Timer();
cycleTimer.Elapsed += new ElapsedEventHandler(OnCycleTimeOut);

//var dataTimer = new System.Timers.Timer();
//dataTimer.Elapsed += new ElapsedEventHandler(OnDataTimeOut);

var cycleStopWatch = new Stopwatch();


try
{
KApiLib.Construct();
GoSdkLib.Construct();
GoSystem system = new GoSystem();
GoSensor sensor;

// Connect to sensor and set data handler
sensor = system.FindSensorById(SensorID);
sensor.Connect();
system.EnableData(true);
system.SetDataHandler(_dataEventHandler.onData);

// Start the sensor/system. After this call, the GoSDK library
// will start receiving data from the sensor. The data handler
// function will be called by the SDK in a separate thread
// asynchonously.
system.Start();


cycleTimer.Interval = (JobTimeOut * 1000);

int firstScanCounter = 0;

while (!cycleTimeOut)
{
_dataEventHandler.DataReceived += OnDataRecieved;

while (dataRecievedFlag == false && !cycleTimeOut)
{
if(firstScanCounter == 0)
{
string message = "Waiting for First Scan";
UpdateProgressTimer((cycleStopWatch.ElapsedMilliseconds), message);
}

if (firstScanCounter == 1)
{
cycleTimer.Enabled = true;
cycleTimer.Start();
cycleStopWatch.Start();
}
try
{
Thread.Sleep(1);
string message = "Waiting for next POT";
UpdateProgressTimer(cycleStopWatch.ElapsedMilliseconds, message);
}
catch (Exception e)
{
Console.WriteLine("Thread was aborted");
}
}

if (dataRecievedFlag)
{
//_returnDataModelList.Add(_singleScannerReturnModel);
Console.WriteLine("-----------Index being added to the model " + _returnDataModelList[dataCounter].FrameIndex);
firstScanCounter++;
string message = "Scanning index " + _singleScannerReturnModel.FrameIndex + " in Tray ---------------------";
Console.WriteLine(message);
UpdateProgressTimer(cycleStopWatch.ElapsedMilliseconds, message);
//OnDataRecieve(this, _singleScannerReturnModel);
Console.WriteLine("Data Points recieved ::" + dataCounter);
dataCounter++;

}

_dataEventHandler.DataReceived -= OnDataRecieved;

//dataTimer.Stop();
//dataTimeOut = false;
dataRecievedFlag = false;
}

OnCompleteScan(_returnDataModelList);
system.Stop();
system.Destroy();
}
catch (KException ex)
{
Console.WriteLine("Error: {0}", ex.ToString());
}

int rowCounter = 0;
foreach(var item in _returnDataModelList)
{
Console.WriteLine("-----------Index being added to the model " + _returnDataModelList[rowCounter].FrameIndex);
rowCounter++;
}
return _returnDataModelList;
}

private void UpdateProgressTimer(long ProgressTimer, string Message)
{
//Console.WriteLine("Progress Timer " + ProgressTimer);
int intervalPercentage = _jobMaxTime;
float currentDuration = ProgressTimer / 1000;
//Console.WriteLine("Divided Timer " + currentDuration);

float percentageCompleted = ((currentDuration / intervalPercentage) * 100);

//Console.WriteLine("Planned % Completed " + percentageCompleted);

if (percentageCompleted > 100)
{
percentageCompleted = 100;
}

OnScanningMessges(Message, Convert.ToInt32(percentageCompleted));
}

public void OnDataRecieved(object source, EventArgs args, ScannerReturnObjectModel returnData)
{
_singleScannerReturnModel = new ScannerReturnObjectModel();
_singleScannerReturnModel = returnData;
_returnDataModelList.Add(_singleScannerReturnModel); //Where the data gets added to List

dataRecievedFlag = true;
Console.WriteLine("Data Recieved");
}
public void OnCycleTimeOut(object source, ElapsedEventArgs e)
{
cycleTimeOut = true;
}
public void OnDataTimeOut(object source, ElapsedEventArgs e)
{
//dataTimeOut = true;
}
protected virtual void OnDataRecieve(object sender, ScannerReturnObjectModel singleScanModel)
{
if (DataReceived != null)
{
DataReceived(this, singleScanModel);
}
}
protected virtual void OnScanningMessges(string Message, int ProgressCounter)
{
if (ScanningProgressMessages != null)
{
ThreadProgressMessagesModel.ProgressMessages = Message;
ThreadProgressMessagesModel.ProgressPercentage = ProgressCounter;
ScanningProgressMessages(Message, ProgressCounter);
}
}

protected virtual void OnCompleteScan(List<ScannerReturnObjectModel> ScanningData)
{
if (ScanComplete != null)
{
ScanComplete(ScanningData);
}
}
}
}

public class OnMeasurementDataRecieveHandler
{
public delegate void DataRecievedHandler(object source, EventArgs args, ScannerReturnObjectModel returnData);

public event DataRecievedHandler DataReceived;

ScannerReturnObjectModel _recievingModel;

int counter = 0;
public OnMeasurementDataRecieveHandler()
{
_recievingModel = new ScannerReturnObjectModel();
_recievingModel.ReturnData = new List<ScannerReturnDataModel>();

}
public void onData(KObject data)
{
_recievingModel.ReturnData.Clear();

GoDataSet dataSet = (GoDataSet)data;
for (UInt32 i = 0; i < dataSet.Count; i++)
{
GoDataMsg dataObj = (GoDataMsg)dataSet.Get(i);
switch (dataObj.MessageType)
{
case GoDataMessageType.Stamp:
{
GoStampMsg stampMsg = (GoStampMsg)dataObj;
for (UInt32 j = 0; j < stampMsg.Count; j++)
{
GoStamp stamp = stampMsg.Get(j);
Console.WriteLine("Frame Index = {0}", stamp.FrameIndex);
_recievingModel.FrameIndex = stamp.FrameIndex;
Console.WriteLine("Time Stamp = {0}", stamp.Timestamp);
_recievingModel.TimeStamp = stamp.Timestamp;
Console.WriteLine("Encoder Value = {0}", stamp.Encoder);
_recievingModel.EncoderValue = stamp.Encoder;
}
}
break;

case GoDataMessageType.Measurement:
{
GoMeasurementMsg measurementMsg = (GoMeasurementMsg)dataObj;
for (UInt32 k = 0; k < measurementMsg.Count; ++k)
{
GoMeasurementData measurementData = measurementMsg.Get(k);
//Console.Write("ID: {0}\t", measurementMsg.Id);
//Console.Write("Value: {0}\t", measurementData.Value);
//onsole.WriteLine("Decision: {0}", measurementData.Decision);
if (measurementMsg.Id == 24) // ratio
{
var _id24Model = new ScannerReturnDataModel();
_id24Model.MessageID = measurementMsg.Id;
_id24Model.DataValue = measurementData.Value;
_id24Model.Decision = Convert.ToBoolean(measurementData.Decision);
_recievingModel.ReturnData.Add(_id24Model);

//ratio_array[m] = measurementData.Value;
}
else if (measurementMsg.Id == 22) // diameter
{
var _id22Model = new ScannerReturnDataModel();
_id22Model.MessageID = measurementMsg.Id;
_id22Model.DataValue = measurementData.Value;
_id22Model.Decision = Convert.ToBoolean(measurementData.Decision);
_recievingModel.ReturnData.Add(_id22Model);
//diameter_array[m] = measurementData.Value;
}
else if (measurementMsg.Id == 21) // height
{
var _id21Model = new ScannerReturnDataModel();

_id21Model.MessageID = measurementMsg.Id;
_id21Model.DataValue = measurementData.Value;
_id21Model.Decision = Convert.ToBoolean(measurementData.Decision);
_recievingModel.ReturnData.Add(_id21Model);
//height_array[m] = measurementData.Value;
}
else if (measurementMsg.Id == 23) // rimwidth
{
var _id23Model = new ScannerReturnDataModel();
_id23Model.MessageID = measurementMsg.Id;
_id23Model.DataValue = measurementData.Value;
_id23Model.Decision = Convert.ToBoolean(measurementData.Decision);
_recievingModel.ReturnData.Add(_id23Model);
//rimwidth_array[m] = measurementData.Value;
}
}
}
break;
}
}
OnDataReceived(_recievingModel);
// Refer to example ReceiveRange, ReceiveProfile, ReceiveMeasurement and ReceiveWholePart on how to receive data
Console.WriteLine(Environment.NewLine);
}

protected virtual void OnDataReceived(ScannerReturnObjectModel ScannerData)
{
var scanData = new ScannerReturnObjectModel();

scanData = ScannerData;
counter++;

if (DataReceived != null)
{
DataReceived(this, EventArgs.Empty, scanData);
}
}
}
OUTPUT Data below

Frame Index = 0
Time Stamp = 14381862912
Encoder Value = 0
OnResponseCounter 1
Data Recieved


-----------Index being added to the model 0
Scanning index 0 in Tray ---------------------
Data Points recieved ::0
Frame Index = 1
Time Stamp = 14382642176
Encoder Value = 0
OnResponseCounter 2
Data Recieved


-----------Index being added to the model 1
Scanning index 1 in Tray ---------------------
Data Points recieved ::1
Frame Index = 2
Time Stamp = 14383028736
Encoder Value = 0
OnResponseCounter 3
Data Recieved


-----------Index being added to the model 2
Scanning index 2 in Tray ---------------------
Data Points recieved ::2
Frame Index = 3
Time Stamp = 14383416320
Encoder Value = 0
OnResponseCounter 4
Data Recieved


-----------Index being added to the model 3
Scanning index 3 in Tray ---------------------
Data Points recieved ::3
Frame Index = 4
Time Stamp = 14383808000
Encoder Value = 0
OnResponseCounter 5
Data Recieved


-----------Index being added to the model 4
Scanning index 4 in Tray ---------------------
Data Points recieved ::4
Frame Index = 5
Time Stamp = 14384195584
Encoder Value = 0
OnResponseCounter 6
Data Recieved


-----------Index being added to the model 5
Scanning index 5 in Tray ---------------------
Data Points recieved ::5
Frame Index = 6
Time Stamp = 14384585728
Encoder Value = 0
OnResponseCounter 7
Data Recieved


-----------Index being added to the model 6
Scanning index 6 in Tray ---------------------
Data Points recieved ::6
Frame Index = 7
Time Stamp = 14384975872
Encoder Value = 0
OnResponseCounter 8
Data Recieved


-----------Index being added to the model 7
Scanning index 7 in Tray ---------------------
Data Points recieved ::7
Frame Index = 8
Time Stamp = 14385362944
Encoder Value = 0
OnResponseCounter 9
Data Recieved


-----------Index being added to the model 8
Scanning index 8 in Tray ---------------------
Data Points recieved ::8
Frame Index = 9
Time Stamp = 14385750528
Encoder Value = 0
OnResponseCounter 10
Data Recieved


-----------Index being added to the model 9
Scanning index 9 in Tray ---------------------
Data Points recieved ::9
Frame Index = 10
Time Stamp = 14386142208
Encoder Value = 0
OnResponseCounter 11
Data Recieved


-----------Index being added to the model 10
Scanning index 10 in Tray ---------------------
Data Points recieved ::10
Frame Index = 11
Time Stamp = 14386528256
Encoder Value = 0
OnResponseCounter 12
Data Recieved


-----------Index being added to the model 11
Scanning index 11 in Tray ---------------------
Data Points recieved ::11
Frame Index = 12
Time Stamp = 14386920448
Encoder Value = 0
OnResponseCounter 13
Data Recieved


-----------Index being added to the model 12
Scanning index 12 in Tray ---------------------
Data Points recieved ::12
Frame Index = 13
Time Stamp = 14387305472
Encoder Value = 0
OnResponseCounter 14
Data Recieved


-----------Index being added to the model 13
Scanning index 13 in Tray ---------------------
Data Points recieved ::13
Frame Index = 14
Time Stamp = 14387695616
Encoder Value = 0
OnResponseCounter 15
Data Recieved


-----------Index being added to the model 14
Scanning index 14 in Tray ---------------------
Data Points recieved ::14
Frame Index = 15
Time Stamp = 14388086784
Encoder Value = 0
OnResponseCounter 16
Data Recieved


-----------Index being added to the model 15
Scanning index 15 in Tray ---------------------
Data Points recieved ::15
Frame Index = 16
Time Stamp = 14388473856
Encoder Value = 0
OnResponseCounter 17
Data Recieved


-----------Index being added to the model 16
Scanning index 16 in Tray ---------------------
Data Points recieved ::16
Frame Index = 17
Time Stamp = 14388861440
Encoder Value = 0
OnResponseCounter 18
Data Recieved


-----------Index being added to the model 17
Scanning index 17 in Tray ---------------------
Data Points recieved ::17
Frame Index = 18
Time Stamp = 14389251584
Encoder Value = 0
OnResponseCounter 19
Data Recieved


-----------Index being added to the model 18
Scanning index 18 in Tray ---------------------
Data Points recieved ::18
Frame Index = 19
Time Stamp = 14389643264
Encoder Value = 0
OnResponseCounter 20
Data Recieved


-----------Index being added to the model 19
Scanning index 19 in Tray ---------------------
Data Points recieved ::19
Frame Index = 20
Time Stamp = 14390031360
Encoder Value = 0
OnResponseCounter 21
Data Recieved


-----------Index being added to the model 20
Scanning index 20 in Tray ---------------------
Data Points recieved ::20
Frame Index = 21
Time Stamp = 14390423552
Encoder Value = 0
OnResponseCounter 22
Data Recieved


-----------Index being added to the model 21
Scanning index 21 in Tray ---------------------
Data Points recieved ::21
Frame Index = 22
Time Stamp = 14390816768
Encoder Value = 0
OnResponseCounter 23
Data Recieved


-----------Index being added to the model 22
Scanning index 22 in Tray ---------------------
Data Points recieved ::22
Frame Index = 23
Time Stamp = 14391213568
Encoder Value = 0
OnResponseCounter 24
Data Recieved


-----------Index being added to the model 23
Scanning index 23 in Tray ---------------------
Data Points recieved ::23
Frame Index = 24
Time Stamp = 14391601152
Encoder Value = 0
OnResponseCounter 25
Data Recieved


-----------Index being added to the model 24
Scanning index 24 in Tray ---------------------
Data Points recieved ::24
The thread 0x7604 has exited with code 0 (0x0).
The thread 0x2b08 has exited with code 0 (0x0).
The thread 0x5a18 has exited with code 0 (0x0).
The thread 0x6eb4 has exited with code 0 (0x0).
-----------Index being added to the model 24
-----------Index being added to the model 24
-----------Index being added to the model 24
-----------Index being added to the model 24
-----------Index being added to the model 24
-----------Index being added to the model 24
-----------Index being added to the model 24
-----------Index being added to the model 24
-----------Index being added to the model 24
-----------Index being added to the model 24
-----------Index being added to the model 24
-----------Index being added to the model 24
-----------Index being added to the model 24
-----------Index being added to the model 24
-----------Index being added to the model 24
-----------Index being added to the model 24
-----------Index being added to the model 24
-----------Index being added to the model 24
-----------Index being added to the model 24
-----------Index being added to the model 24
-----------Index being added to the model 24
-----------Index being added to the model 24
-----------Index being added to the model 24
-----------Index being added to the model 24
-----------Index being added to the model 24






labjac

Continue reading...
 
Back
Top