Solved

Advanced Multi threaded application

Posted on 2004-04-09
6
234 Views
Last Modified: 2010-04-05
The last answer was good (with Application.ProcessMessages )

Put If you open all the forms the counter for each will start processing after the other finish.

It dosent seem to be a thread for the generated form ??

Please try it

Omar Fayyad
0
Comment
Question by:omarmf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10793403
OMar,

I have made Some changes: see code below (I have also emailed you a copy):

Note: A thread is a timeslice of the Main process (CPU). When you spawn a thread, and then spawn another, the newest thread will have the priority, therefore, you have to explicity tell it to share the priority.

Set the priority for each thread to normal

AThread.Priority:= tpNormal;

I have also added a private variable to the Thread class, to store the count value

Shane





unit nismsn;

interface

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

type

  TXThread = class(TThread)
  private
    { Private declarations }
   fNode: TTreeNode;
   fName: String;
   fForm: TForm;
   fLabel: TLabel;
   fButton: TButton;
   fCount: Integer;
  protected
    procedure DoCount;
    procedure Execute; override;
  public
   property Count: Integer read fCount write fCount;
   property Name: String read fName write fName;
   property ThreadNode: TTreeNode read fNode write fNode;
   property ThreadForm: TForm read fFOrm write fForm;
   property FormLabel: TLabel read fLabel write fLabel;
   property FormButton: TButton read fButton write fButton;
   constructor Create(ANode: TTreeNode; AForm: TForm; ALabel:
                      TLabel; AButton: TButton);reintroduce;
 end;

  TForm1 = class(TForm)
    TreeView1: TTreeView;
    Timer: TTimer;
    senderm: TEdit;
    ListBox1: TListBox;
    Label1: TLabel;
    BitBtn1: TBitBtn;
    Button1: TButton;
    R_ID: TEdit;
    S_ID: TEdit;
    ImageList1: TImageList;
    FontDialog1: TFontDialog;
    recieverm: TRichEdit;
    procedure sendermKeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure TreeView1DblClick(Sender: TObject);
    procedure CustomFormOnClose(Sender: TObject; var Action: TCloseAction);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure BtnClickEv(Sender: TObject);
  private
    { Private declarations }
  public
      MsgL: TStringList;
      ThreadList: TObjectList;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TXThread.DoCount;
begin
 FormLabel.Caption:= inttostr(Count);
end;

procedure TXThread.Execute;
var I: Integer;
begin
  for I:= 0 to 10000 do
  begin
   Count:= I;
   Synchronize(DoCount);
   Sleep(20);
  end;
end;

constructor TXThread.Create(ANode: TTreeNode; AForm: TForm;
                            ALabel: TLabel; AButton: TButton);
begin
inherited Create(False);
 fNode:= ANode;
 fForm:= AForm;
 fLabel:= ALabel;
 fButton:= AButton;
end;

procedure TForm1.CustomFormOnClose(Sender: TObject; var Action: TCloseAction);
Begin
  TForm(Sender).Destroy;
end;

procedure TForm1.sendermKeyPress(Sender: TObject; var Key: Char);
begin
If Key = #13 Then
 begin
  if(senderm.Text<>'') Then
  MsgL.Add(senderm.Text);
  ListBox1.Items:= MsgL;
  senderm.Text:= '';
 end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  TreeNodeM1, TreeNodeM2: TTreeNode;
begin
  MsgL:= TStringList.Create;
  ThreadList:= TObjectList.Create;
  ThreadList.OwnsObjects:= True;
  TreeNodeM1 := TreeView1.Items.Add(nil, 'Online');
  TreeNodeM2 := TreeView1.Items.Add(nil, 'Offline');
  TreeView1.Items.AddChild(TreeNodeM2, 'Omar');
  TreeView1.Items.AddChild(TreeNodeM1, 'Abdu');
  TreeView1.Items.AddChild(TreeNodeM1, 'Modar');
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  if(senderm.Text<>'') Then
    Begin
      MsgL.Add(senderm.Text);
      ListBox1.Items:= MsgL;
      senderm.Text:= '';
    End;
end;

procedure TForm1.BtnClickEv(Sender: TObject);
begin
  ShowMessage(IntToStr(TForm(TBitBtn(Sender).Parent.Name).ComponentCount));
  ShowMessage(TBitBtn(Sender).Name);
  ShowMessage(TBitBtn(Sender).Parent.Name);
  //ShowMessage(TForm(TBitBtn(Sender).Parent.Name).Components[4].Name);
end;

procedure TForm1.TreeView1DblClick(Sender: TObject);
var
  NewForm : TForm;
  RichEdit: TRichEdit;
    Edit  : TEdit;
  NewButton: TBitBtn;
  NewLabel: TLabel;
  AThread : TXThread;
begin
  NewForm := TForm.create(self);
  with NewForm do
  begin
    Left := 100;
    Top := 200;
    Width := 250;
    Height := 350;
    Caption := TreeView1.Selected.Text;
    Name   := 'Form'+TreeView1.Selected.Text;
    OnClose := CustomFormOnClose;
    Show;
  end;
  RichEdit:= TRichEdit.Create(Self);
  with RichEdit do
  begin
    Left := 5;
    Top := 5;
    Width := 180;
    Height := NewForm.Height - 60;
    text:= '';
    Name   := 'RichEdit'+TreeView1.Selected.Text;
    parent := NewForm;
  end;
  Edit:= TEdit.Create(Self);
  with Edit do
  begin
    Left := RichEdit.Left;
    Width := RichEdit.Width;
    Height := 300;
    Top := RichEdit.Height+10;
    Name   := 'Edit'+TreeView1.Selected.Text;
    text := '';
    parent := NewForm;
  end;
  NewButton:= TBitBtn.Create(Self);
  with NewButton do
  begin
    Left := edit.width + 10;
    Width := 50;
    Height := Edit.Height;
    Top := Edit.Top;
    Name   := 'BitBtn'+TreeView1.Selected.Text;
    Caption:= 'Send';
    OnClick:= BtnClickEv;
    parent := NewForm;
  end;
  NewLabel:= TLabel.Create(Self);
  with NewLabel do
  begin
    Left := edit.width + 30;
    Width := 50;
    Height := Edit.Height;
    Top := 30;
    Name   := 'Caption'+TreeView1.Selected.Text;
    Caption:= '0';
    parent := NewForm;
  end;
  AThread:= TXThread.Create(TreeView1.Selected, NewForm, NewLabel, NewButton);
  AThread.Priority:= tpNormal;
  AThread.Name:= 'Thread'+ AThread.ThreadNode.Text;
  ThreadList.Add(AThread);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 ThreadList.Free;
end;

end.
0
 

Author Comment

by:omarmf
ID: 10794223
Fantastic !!! ????????

Great answer

I thought that i am good in delphi before

i have One more thing I need from you shane.

I will open new quistion for it

Thank you

Omar
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10794282
K, i will be keeping my eye out!

Shane
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:omarmf
ID: 10794344
I mailed you the updated file

in the created form there is start and stop

I dont know what to do to add stop thread to strat or stop the task in the form.

Please help
0
 

Author Comment

by:omarmf
ID: 10794364
I mailed you the updated file

in the created form there is start and stop

I dont know what to do to add stop thread to strat or stop the task in the form.

Please help
0
 
LVL 11

Accepted Solution

by:
shaneholmes earned 200 total points
ID: 10794369
Please close this question and open a new one!

And in all fairness, explain it to all experts, so as all with have a chance...

Thanks

Shane
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question