Advertisement

02.09.2008 at 07:01AM PST, ID: 23150078
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Add to and Clear TObjectList from a Thread

Tags: Delphi
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
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
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.
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: Romans
Solution Provided By: ziolko
Participating Experts: 1
Solution Grade: A
Views: 21
Translate:
Loading Advertisement...
02.09.2008 at 12:40PM PST, ID: 20858564

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.09.2008 at 02:00PM PST, ID: 20858877

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.09.2008 at 02:09PM PST, ID: 20858936

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.09.2008 at 04:00PM PST, ID: 20859265

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.09.2008 at 04:17PM PST, ID: 20859322

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.09.2008 at 04:23PM PST, ID: 20859338

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.09.2008 at 04:30PM PST, ID: 20859355

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.13.2008 at 12:16AM PST, ID: 20882494

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.13.2008 at 04:40AM PST, ID: 20883601

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.13.2008 at 08:53AM PST, ID: 20885836

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • Automotive
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Displays / Monitors
  • Handhelds / PDAs
  • Components
  • Peripherals
  • Laptops/Notebooks
  • Servers
  • Misc
  • Apple
  • Embedded Hardware
  • Networking Hardware
  • Storage
  • Desktops
  • New Users
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMware
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Virtualization
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • Web Computing
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Consulting
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMware
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Automation
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Web Services
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Web Computing
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Lounge
  • Business Travel
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
  • Automotive
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
02.09.2008 at 12:40PM PST, ID: 20858564

Rank: Sage

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.
 
02.09.2008 at 02:00PM PST, ID: 20858877
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
 
02.09.2008 at 02:09PM PST, ID: 20858936
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
 
02.09.2008 at 04:00PM PST, ID: 20859265

Rank: Sage

>>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.
 
02.09.2008 at 04:17PM PST, ID: 20859322

Rank: Sage

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.
 
02.09.2008 at 04:23PM PST, ID: 20859338

Rank: Sage

.... 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.
 
02.09.2008 at 04:30PM PST, ID: 20859355

Rank: Sage

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.
 
02.13.2008 at 12:16AM PST, ID: 20882494

Rank: Sage

any progress ?

ziolko.
 
02.13.2008 at 04:40AM PST, ID: 20883601
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.
 
02.13.2008 at 08:53AM PST, ID: 20885836

Rank: Sage

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.
Accepted Solution
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628