Solved

Advanced Multi threaded application

Posted on 2004-04-09
6
229 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
  • 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now