I have written a small application that sends an Email.
I would like it to terminate itself without any user action.
I end the OnCreate procedure with Self.Close, but the application keeps running.
What am i missing?

Worst/Quickest/Unelegant method? (assuming you don't just make a console app)

1. Drop a TTimer on the form
2. In the OnTimer event:
Timer1.enabled=false;
SendEmail(); <-- whatever it is you do
self.close

Better/More elegant: If your email has an event "OnSent" or something, you could call self.close there.

Best: console application. Just have it open, create the email components, fire and close.

Thanks for your reply. :)
There is an ondisconnected event, but it didn't work. The Timer solution did.
Agree about console solution, but i don't have the knowledge to do it.

The best way to terminate an application is to call application.terminate This prevents a lot of problems and can be called anywhere.

Avoid using tTimer as much as possible because it is a "heavy" object --chewing up resources and time.

Hope this helps.

The best way to terminate an application is to call application.terminate This prevents a lot of problems and can be called anywhere.

Avoid using tTimer as much as possible because it is a "heavy" object --chewing up resources and time.

Hope this helps.

Hello and thanks for your reply.
I tried the Application.Terminate (think it's still in there) but it didn't terminate the application.
As it is the Ontimer seems to be the only solution that works. Resources are not a problem since the app. is very small and fast executing.

I'd be interested to know why application.terminate didn't stop your program. Have you got unusual hooks in there somewhere? Did your code actually call it?

The problem with the timer is that there is a system limit on the number of timers available. It doesn't matter how big your application is.

I'd be interested to know why application.terminate didn't stop your program. Have you got unusual hooks in there somewhere? Did your code actually call it?

The problem with the timer is that there is a system limit on the number of timers available. It doesn't matter how big your application is.

I had the Application.Terminate in the wrong place it seems. (After Application.Run).
Moving it to the end of the OnCreate (where all the work is done) solved the problem.
Thanks for you'r suggestion. :)

Ah, yes. That would do it. ;) Glad you caught that.

Guess you are not looking for an alternate solution any more but in case you are - the best thing to do is to create a console app. You should only need to code one unit and from there do the email sending in the initialization section, do any residual housekeeping in the finalization section and you are done.

If you are still going to mess with it...

Forget the console app, initialization and finalization, and all those other loops.

Just don't bother creating any forms. Don't call application.run.
Just do your stuff in the application .dpr file's begin..end block, and let the program terminate naturally when it hits the end.

program fooey;
uses Classes;
var
  foo: tFileStream;
  s: string = 'Hello, world!';
begin
foo := tFileStream.create( 'fooey.txt', fmCreate or fmOpenWrite );
foo.write( pChar( s )^, length( s ) )
end.

Good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.