Add to and Clear TObjectList from a Thread

Experts,
I have an TObject class (TBreakData) and a TObjectList (MyBreakList). MyBreakList is a list of BreakData objects and they are displayed in a Virtual list view.

The example code has a List View (two columns) with OwnerData = True and ViewStyle = vsReport. Two buttons - Button1 creates and adds data to MyBreakList and Button2 deletes the data.

Ok - so here is the question:
I would like to create BreakData Objects, Add them to MyBreakList all FROM A THREAD. The thread must first clear MyBreakList and the ListView on the form before adding new BreakData.

I have tried a few things but have not been able to do it. I am not very familiar with threads so I would appreciate some help in writing a thread that does this - please help.

Thanks
Romans
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Contnrs, ComCtrls, StdCtrls;
 
type
TBreakData = class(TObject)
    BreakNo         : Integer;
    BreakText       : String;
end;
 
type
  TForm1 = class(TForm)
    ListView1: TListView;
    Add: TButton;
    Remove: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ListView1Data(Sender: TObject; Item: TListItem);
    procedure AddClick(Sender: TObject);
    procedure RemoveClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    MyBreakList:    TObjectList;
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.FormCreate(Sender: TObject);
begin
    MyBreakList := TObjectList.Create;
end;
 
procedure TForm1.FormDestroy(Sender: TObject);
begin
    MyBreakList.Free;
end;
 
procedure TForm1.ListView1Data(Sender: TObject; Item: TListItem);
begin
    if TBreakData(MyBreakList[Item.Index]).BreakText <> '' then
    begin
        Item.Caption := IntToStr(TBreakData(MyBreakList[Item.Index]).BreakNo);
        Item.SubItems.Add(TBreakData(MyBreakList[Item.Index]).BreakText);
    end;
end;
 
procedure TForm1.AddClick(Sender: TObject);
Var
    MyBreak:    TBreakData;
    i:          Integer;
begin
    for i := 1 to 150 do
    begin
        MyBreak := TBreakData.Create;
        MyBreak.BreakNo := i;
        MyBreak.BreakText := 'ExampleText: This is break number ' + IntToStr(i);
        MyBreakList.Add(MyBreak);
    end;
 
    ListView1.Items.Count := MyBreakList.Count;
end;
 
procedure TForm1.RemoveClick(Sender: TObject);
begin
    ListView1.Items.Clear;
    MyBreakList.Clear;
    ListView1.Items.Count := MyBreakList.Count;
end;
 
end.

Open in new window

RomansAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ziolkoCommented:
onr of few ways to do it:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, Contnrs;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FObjects: TObjectList;
  public
    { Public declarations }
  end;

  TBreakData = class(TObject)
  private
    FBreakNo: Integer;
    FBreakText: string;
  public
    constructor Create(ABreakNo: Integer;const ABreakText: string);
    property BreakNo: Integer read FBreakNo;
    property BreakText: string read FBreakText;
  end;


  TMyThread = class(TThread)
  private
    FListItemsRef: TListItems;
    FObjectsRef: TObjectList;
    procedure CreateObjects;
    procedure FillListView;
  protected
    constructor Create(AListItems: TListItems;AObjects: TObjectList);reintroduce;
    procedure Execute;override;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TBreakData }

constructor TBreakData.Create(ABreakNo: Integer; const ABreakText: string);
begin
  inherited Create;
  FBreakNo := ABreakNo;
  FBreakText := ABreakText;
end;

{ TMyThread }

constructor TMyThread.Create(AListItems: TListItems;
  AObjects: TObjectList);
begin
  inherited Create(False);
  FreeOnTerminate := True;
  FListItemsRef := AListItems;
  FObjectsRef := AObjects;
end;

procedure TMyThread.CreateObjects;
var cnt: Integer;
begin
  if Assigned(FObjectsRef) then
    for cnt := 0 to 150 do
      FObjectsRef.Add(TBreakData.Create(cnt, IntToStr(cnt)));
end;

procedure TMyThread.Execute;
begin
  CreateObjects;
  Synchronize(FillListView);
end;

procedure TMyThread.FillListView;
var cnt: Integer;
begin
  if Assigned(FListItemsRef) and Assigned(FObjectsRef) then begin
    FListItemsRef.BeginUpdate;
    try
      for cnt := 0 to FObjectsRef.Count - 1 do
        with FListItemsRef.Add do begin
          Caption := IntToStr(TBreakData(FObjectsRef[cnt]).BreakNo);
          SubItems.Add(TBreakData(FObjectsRef[cnt]).BreakText);
        end;
    finally
      FListItemsRef.EndUpdate;
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FObjects := TObjectList.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FreeAndNil(FObjects);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  TMyThread.Create(ListView1.Items, FObjects);
end;

end.

ziolko.
0
RomansAuthor Commented:
ziolko,
Thanks very much - this appears to work. I am still digesting it to try understand it completly. I am not very familiar with threads.

One more question - when the thread is executed a second time it simply adds an additional list of objects without clearing the listview and objectlist first.

Can you help me to clear it all before the thread runs a second time?

Romans
0
RomansAuthor Commented:
ziolko,
Do me a favor and fill this with 150000 items. This takes some time to process and you find that the screen freezes while the list view is being filled. Perhaps don't understand threads - I thought this should be able to run while letting the main thread (GUI) continue uninterupted.

Unfortunatly - this will not work for what I am trying to do. Do you have other suggestions?

Romans
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

ziolkoCommented:
>>when the thread is executed a second time it simply adds an additional list of objects without clearing the listview and objectlist first.

little correction:

procedure TMyThread.CreateObjects;
var cnt: Integer;
begin
  if Assigned(FObjectsRef) then begin
    FObjectsRef.Clear;
    for cnt := 0 to 150 do
      FObjectsRef.Add(TBreakData.Create(cnt, IntToStr(cnt)));
  end;
end;

procedure TMyThread.FillListView;
var cnt: Integer;
begin
  if Assigned(FListItemsRef) and Assigned(FObjectsRef) then begin
    FListItemsRef.BeginUpdate;
    try
      FListItemsRef.Clear;
      for cnt := 0 to FObjectsRef.Count - 1 do
        with FListItemsRef.Add do begin
          Caption := IntToStr(TBreakData(FObjectsRef[cnt]).BreakNo);
          SubItems.Add(TBreakData(FObjectsRef[cnt]).BreakText);
        end;
    finally
      FListItemsRef.EndUpdate;
    end;
  end;
end;

>>This takes some time to process and you find that the screen freezes while the list view is being filled.

ok, I'll try to explain this one.
TListView was created and exists in main thread but you fill it from other thread so you have situation when two threads access same memory. To avoid AccessViolations you need to synchronize (see .Execute method of TMyThread) unfortunatelly TListView and TTreeView doesn't handle large number of items too quickly that's why app frozes. Of course you can get rid of synchronization part so your app will respond (but still it will take some time to load large number of items) however not using synchronization puts you in big risk of getting AccessViolation errors and it's not recommended.

>>Unfortunatly - this will not work for what I am trying to do. Do you have other suggestions?

threads can be used on many different ways and it really depends on what you trying to do, there's no one general step-by-step recipe how to use threads. for instance if you have two listboxes it might be good idea to fill them with two threads so they're filled simultaniously. other scenario is when items must "produceed" by reading bunch of files or reading data from network, device over COM port, etc. this is also good indication to use thread.

ziolko.
0
ziolkoCommented:
here's other approach to problem, maybe this will be better for you:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, Contnrs;

const
  WM_ADDITEM = WM_USER + $0001;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FObjects: TObjectList;
    procedure AddItemMessage(var Msg: TMessage); message WM_ADDITEM;
  public
    { Public declarations }
  end;

  TBreakData = class(TObject)
  private
    FBreakNo: Integer;
    FBreakText: string;
  public
    constructor Create(ABreakNo: Integer;const ABreakText: string);
    property BreakNo: Integer read FBreakNo;
    property BreakText: string read FBreakText;
  end;


  TMyThread = class(TThread)
  private
    FObjectsRef: TObjectList;
    FNotify: THandle;
    procedure CreateObjects;
    procedure FillListView;
  protected
    constructor Create(ANotify:THandle;AObjects: TObjectList);reintroduce;
    procedure Execute;override;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TBreakData }

constructor TBreakData.Create(ABreakNo: Integer; const ABreakText: string);
begin
  inherited Create;
  FBreakNo := ABreakNo;
  FBreakText := ABreakText;
end;

{ TMyThread }

constructor TMyThread.Create(ANotify:THandle;AObjects: TObjectList);
begin
  inherited Create(False);
  FreeOnTerminate := True;
  FNotify := ANotify;
  FObjectsRef := AObjects;
end;

procedure TMyThread.CreateObjects;
var cnt: Integer;
begin
  if Assigned(FObjectsRef) then begin
    FObjectsRef.Clear;
    for cnt := 0 to 15000 do
      FObjectsRef.Add(TBreakData.Create(cnt, IntToStr(cnt)));
  end;
end;

procedure TMyThread.Execute;
begin
  CreateObjects;
  FillListView;
end;

procedure TMyThread.FillListView;
var cnt: Integer;
begin
  if Assigned(FObjectsRef) then begin
    for cnt := 0 to FObjectsRef.Count - 1 do begin
      PostMessage(FNotify, WM_ADDITEM, cnt, 0);
      Sleep(1);
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FObjects := TObjectList.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FreeAndNil(FObjects);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  TMyThread.Create(Handle, FObjects);
end;

procedure TForm1.AddItemMessage(var Msg: TMessage);
var it: TListItem;
    obj: TBreakData;
begin
  it := ListView1.Items.Add;
  obj := TBreakData(FObjects[Msg.WParam]);
  it.Caption := IntToStr(obj.BreakNo);
  it.SubItems.Add(obj.BreakText);
end;

end.

ziolko.
0
ziolkoCommented:
.... forgot clearing stuff again.. sorry.

procedure TForm1.Button1Click(Sender: TObject);
begin
  ListView1.Items.BeginUpdate;
  try
    ListView1.Items.Clear;
  finally
    ListView1.Items.EndUpdate;
  end;
  TMyThread.Create(Handle, FObjects);
end;


ziolko.
0
ziolkoCommented:
and also some extra precoutions:

procedure TForm1.AddItemMessage(var Msg: TMessage);
var it: TListItem;
    obj: TBreakData;
begin
  if (Msg.WParam >= 0) and (Msg.WParam < FObjects.Count) then begin
    it := ListView1.Items.Add;
    obj := TBreakData(FObjects[Msg.WParam]);
    it.Caption := IntToStr(obj.BreakNo);
    it.SubItems.Add(obj.BreakText);
  end;
end;


sorry for posting in pieces but it's 1.30am  and my mind doesn't work fast enough (just like TListView:-) )

ziolko.
0
ziolkoCommented:
any progress ?

ziolko.
0
RomansAuthor Commented:
ziolko,
I looked at it over the weekend but am away on business this week. Will get back to it again when I return home at the end of the week.
0
ziolkoCommented:
Hi, didn't mean to push you but there's a lot of abandoned Qs so I just wanted to know if I should keep test project codes:)

ziolko.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RomansAuthor Commented:
Thanks for the code - I think this will work well.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.