Advanced Multi threaded application

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
omarmfAsked:
Who is Participating?
 
shaneholmesCommented:
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
 
shaneholmesCommented:
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
 
omarmfAuthor Commented:
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
shaneholmesCommented:
K, i will be keeping my eye out!

Shane
0
 
omarmfAuthor Commented:
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
 
omarmfAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.