Author Topic: Adding a StringList to NrHID Thread for Bulk Readings ?  (Read 1866 times)

moelski

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Adding a StringList to NrHID Thread for Bulk Readings ?
« on: September 19, 2009, 09:01:00 am »
Hi Roman,

finally I got my data reading to work. You know I had a strange (Thread) problem which I posted here:
http://forums.nrcommlib.com/index.php?topic=139.0

Now I use a Custom Windows Message which is (in my opinion) an excelent way to get the data from the Thread to your mainthread / main Application. btw. Thx for the suggestion!

But I have an disadvantage while receiving a hugh amount of data (bulk import of measured data from the device). I use this code to store bulk data:
Code: [Select]
 for _i := 0 to Received - 1
    do _s := _s + PAnsiChar(Buffer)[_i];

  Incoming.Add(_S);  


That´s no big problem, but Incoming is defined in main:
Code: [Select]
var
  Form1       : TForm1;
 Incoming    : TStringList;

This will work if you don´t access the Incoming StringList from the Main Thread. I use it only in the HID Thread for storing bulk data.
To decode the bulk data (at the end of receiving the data) I use an extra pointer and NOT the incoming StringList:
Code: [Select]
   New(_msg);
    _msg.Data := TStringList.Create;
    _msg.Data.Assign(Incoming);
    PostMessage(self.Handle, WM_DECODE_MESSAGE, 0, Integer(_msg));

So as I said before this works perfectly. But I think it´s not a really good way.
Do you see any chance to get a StringList in the Thread which can freely used by the programmer?
This would have a big advantage ... The data is stored directly in the Thread and not in the Mainthread.
After receiving all the bulk data I could use my pointer (and the windows message) to deliver the data to the main Thread.

So the only thing you have to do is adding a stringlist to the Thread and make it accessable for the normal programmer.

What did you think about the suggestion?

Greetz Dominik
« Last Edit: September 19, 2009, 09:03:23 am by moelski »

Roman Novgorodov

  • Engineer
  • Administrator
  • Hero Member
  • *****
  • Posts: 994
  • we will win
    • View Profile
    • Forums.nrCommLib.Com
Re: Adding a StringList to NrHID Thread for Bulk Readings ?
« Reply #1 on: September 21, 2009, 03:15:12 am »
Hello

You can try to use a critical secetion for lock nonthreadsafe object. Plese see following sample:

Code: [Select]

var cs : TCriticalSection; // somewhere in global scope

procedure TForm1.FormCreate(Sender: TObject);
begin
  cs := TCriticalSection.Create();
end;

...

  for _i := 0 to Received - 1
    do _s := _s + PAnsiChar(Buffer)[_i];

  cs.Enter;
  Incoming.Add(_S); 
  cs.Leave;

...

   //New(_msg);
  //_msg.Data := TStringList.Create;
  // _msg.Data.Assign(Incoming);
  _msg.Data := Incoming;

  cs.Enter; // but!!!! maybe better to use cs not here!!! use it in WM_DECODE_MESSAGE handler for access list
   PostMessage(self.Handle, WM_DECODE_MESSAGE, 0, Integer(_msg));
  cs.Leave;

.....

PLease see my last comment in sample code.

Roman Novogrodov
DeepSoftware llc - The professional components for Delphi/CBuilder/.NET. The high quality custom software development.
Forums.nrCommLib.Com - DeepSoftware Tech Support Forum.