Solved

Handling Object events within project source.

Posted on 2000-04-18
5
152 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 50 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

16 Experts available now in Live!

Get 1:1 Help Now