Solved

Stop, start and reume the task

Posted on 2004-04-09
2
241 Views
Last Modified: 2010-04-05
in the created form there is start and stop buttons

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

please advice

Omar fayyad

0
Comment
Question by:omarmf
2 Comments
 
LVL 11

Accepted Solution

by:
shaneholmes earned 125 total points
ID: 10794878
Omar, I emailed you the code.

As I stated in the last post, you need to explain you rnew posts better so as other experts have a shot at helping you solve your questions. Although, I understood (cause I have worked on it enough), a EE expert will need a full explanation.

ANyway, here is the code below for start & stop of your threads.

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;
   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;
   constructor Create(ANode: TTreeNode; AForm: TForm; ALabel:
                      TLabel);reintroduce;
 end;

  TForm1 = class(TForm)
    TreeView1: TTreeView;
    recieverm: TRichEdit;
    procedure FormCreate(Sender: TObject);
    procedure TreeView1DblClick(Sender: TObject);
    procedure CustomFormOnClose(Sender: TObject; var Action: TCloseAction);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Btn1ClickEv(Sender: TObject);
    procedure Btn2ClickEv(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);
begin
inherited Create(True);
 fNode:= ANode;
 fForm:= AForm;
 fLabel:= ALabel;
end;

procedure TForm1.CustomFormOnClose(Sender: TObject; var Action: TCloseAction);
Begin
  TForm(Sender).Destroy;
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.Btn1ClickEv(Sender: TObject);
var
 Idx: Integer;
begin
 Idx:= TButton(Sender).Parent.Tag;
 TXThread(ThreadList[Idx]).Resume;
end;

procedure TForm1.Btn2ClickEv(Sender: TObject);
var
 Idx: Integer;
begin
 Idx:= TButton(Sender).Parent.Tag;
 TXThread(ThreadList[Idx]).Suspend;
end;

procedure TForm1.TreeView1DblClick(Sender: TObject);
var
  NewForm : TForm;
  RichEdit: TRichEdit;
    Edit  : TEdit;
  NewButton1, NewButton2: TBitBtn;
  NewLabel: TLabel;
  AThread : TXThread;
begin
  NewForm := TForm.create(Application); //*****
  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;
  NewButton1:= TBitBtn.Create(Self);
  with NewButton1 do
  begin
    Left := edit.width + 10;
    Width := 50;
    Height := Edit.Height;
    Top := Edit.Top;
    Name   := 'BitBtn1'+TreeView1.Selected.Text;
    Caption:= 'Start';
    OnClick:= Btn1ClickEv;
    parent := NewForm;
  end;
  NewButton2:= TBitBtn.Create(Self);
  with NewButton2 do
  begin
    Left := edit.width + 10;
    Width := 50;
    Height := Edit.Height;
    Top := Edit.Top - 30;
    Name   := 'BitBtn2'+TreeView1.Selected.Text;
    Caption:= 'Stop';
    OnClick:= Btn2ClickEv;
    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);
  AThread.Priority:= tpNormal;
  AThread.Name:= 'Thread'+ AThread.ThreadNode.Text;
  NewForm.Tag:= ThreadList.Add(AThread); //*****
end;

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

end.
0
 

Author Comment

by:omarmf
ID: 10796175
Ok nice

  I want it to start working directly so i madeworking with for

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

It works ??

0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Comparing dates in Delphi. Greater than/ Less than 3 154
Working with hours 3 58
Performance of SQL statement 37 111
Delphi and Access based Enumeration 9 64
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

777 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