Solved

Stop, start and reume the task

Posted on 2004-04-09
2
246 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
[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
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Suggested Courses
Course of the Month7 days, 15 hours left to enroll

617 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