Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Handling Object events within project source.

Posted on 2000-04-18
5
Medium Priority
?
183 Views
Last Modified: 2010-04-04
I've got a simple project (NO forms) and I've got a TTimer in it.  How do I handle the events within the Project's source?

John.
0
Comment
Question by:Jaymol
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:shenqw
ID: 2727533
//Unit1

unit Unit1;

interface

uses
  extctrls,
  Forms;

type
  MyTest=class(TObject)
  private
    Text:TextFile;
    bExit:boolean;
    iCount:integer;
    Timer:TTimer;
    procedure TimerEvent(Sender:TObject);
  public
    constructor Create;
    destructor  Destroy;override;
    procedure Run;
  end;

var
  Test:MyTest;

implementation

{ MyTest }

constructor MyTest.Create;
begin
  inherited;
  bExit:=False;
  iCount:=0;
  AssignFile(Text,'c:\Temp\Test.Text');
  Rewrite(Text);
  Timer:=TTimer.Create(nil);
  Timer.OnTimer:=TimerEvent;
  Timer.Interval:=1000;
  Timer.Enabled:=True;
end;

destructor MyTest.Destroy;
begin
  CloseFile(Text);
  Timer.Free;Timer:=nil;
  inherited;
end;

procedure MyTest.Run;
begin
  while not bExit do Application.ProcessMessages;
end;

procedure MyTest.TimerEvent(Sender: TObject);
begin
  Inc(iCount);
  writeln(Text,iCount);
  if iCount>100 then bExit:=True;
end;

end.


//Main Unit

program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas';

{$R *.RES}

begin
  Application.Initialize;
  Test:=MyTest.Create;
  Test.Run;
end.
0
 
LVL 6

Author Comment

by:Jaymol
ID: 2729744
Thanks for the suggestion Shenqw, but that's not what I want.

I want to handle the events within the project source - not elsewhere.

Thanks,

John.
0
 
LVL 3

Accepted Solution

by:
shenqw earned 200 total points
ID: 2729835
//try follow

program Project2;

uses
  extctrls,
  Forms;

type
  MyTest=class(TObject)
  private
    Text:TextFile;
    bExit:boolean;
    iCount:integer;
    Timer:TTimer;
    procedure TimerEvent(Sender:TObject);
  public
    constructor Create;
    destructor  Destroy;override;
    procedure Run;
  end;

var
  Test:MyTest;

{$R *.RES}

{ MyTest }

constructor MyTest.Create;
begin
  inherited;
  bExit:=False;
  iCount:=0;
  AssignFile(Text,'c:\Temp\Test.txt');
  Rewrite(Text);
  Timer:=TTimer.Create(nil);
  Timer.OnTimer:=TimerEvent;
  Timer.Interval:=100;
  Timer.Enabled:=True;
end;

destructor MyTest.Destroy;
begin
  CloseFile(Text);
  Timer.Free;Timer:=nil;
  inherited;
end;

procedure MyTest.Run;
begin
  while not bExit do Application.ProcessMessages;
end;

procedure MyTest.TimerEvent(Sender: TObject);
begin
  Inc(iCount);
  writeln(Text,iCount);
  if iCount>100 then bExit:=True;
end;

begin
  Application.Initialize;
  Test:=MyTest.Create;
  Test.Run;
  Test.Free;
end.


0
 

Expert Comment

by:dentener
ID: 2729854
Try something like this:

1. Create a small object withing your project source and add the method below:

procedure MessageHandler(var Msg: TMsg; var Handled: Boolean);

2. In your projectsource add:

SetTimer(Application.Handle, 1, INTERVAL, nil);

3. Create the small object and connect Application.OnMessage with the MessageHandler method

4. Add some code like the code below to the MessageHandler method

  with Msg do
    if Msg = WM_TIMER then
    begin
      Handled:= true;
      ......
    end
    else handled:= false;

5. Finnaly add KillTimer(Application.Handle, 1) somewhere after Application.Run;


I Really don't know if this works, you will have to try it...

Regards,

Paul
0
 
LVL 6

Author Comment

by:Jaymol
ID: 2730095
Thanks Shenqw.  That's just what I was after!

Thanks everyone else for your input, but Shenqw gave the best answer 1st.

Ta,

John.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

972 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