Solved

Stop, start and reume the task

Posted on 2004-04-09
2
242 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Title # Comments Views Activity
delphi prevent click fast 2 240
Delphi: ForceDirectory plain function 7 42
Dev express lookupcombo 3 35
enhance the following code 3 37
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 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