Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: How can I implement a Timer?


This question is not answered. Helpful answers available: 0. Correct answers available: 1.


Permlink Replies: 28 - Last Post: Feb 8, 2018 4:06 PM Last Post By: Alexandre Machado
Erwin Schwarz

Posts: 11
Registered: 2/26/18
How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 28, 2018 5:09 PM
Hi, I have tried countless ways to implement a timer routine inside a IW application without any luck (at the ServerController, inside a DataModule created by the ServerController or created directly at the main program, etc). The thing is that when running inside Delphi everything works great, but when I install it as a windows service, the timer won't work... if I open a browser, the page will display correctly (so I know the windows service is working), but the timer don't work. As I told you, I tried using a DataModule, so I wrote a .log file to verify what was happening: when I run the .exe file (with the /gui parameter) all works fine, but when I install it as a service the timer won't work and the log file shows me that the DataModule was destroyed. Any ideas on how to do this? or maybe it is not possible to implement a timer using IntraWeb as a windows service? Thank you!

Edited by: Erwin Schwarz on Jan 28, 2018 5:12 PM
Eitan Arbel

Posts: 508
Registered: 2/24/13
Re: How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 28, 2018 9:17 PM   in response to: Erwin Schwarz in response to: Erwin Schwarz
hi Erwin

did you try in both Sync and ASync ?
is your IWTimer set to Active in Design time? what if you Activate it when the app is running, before you need it ?

just a side note -
"...the log file shows me that the DataModule was destroyed"
the UserSessionUnit is a DataModule.
why do you need to have both of them ?

Dan Barclay

Posts: 889
Registered: 11/9/03
Re: How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 28, 2018 9:37 PM   in response to: Erwin Schwarz in response to: Erwin Schwarz
It sounds like you may be trying to use a regular Delphi timer.

Etian's response refers to an IW timer (TIWTimer) which you would place on a form. It creates timer events (either sync or async) which will execute based on messages to the server.

Maybe it would help to know what you're using the timer for. It might help in coming up with an option that will work for you.

I'd suggest, in general, that the server just respond to session messages rather than using much internally generated operations. The TIWTimer accomplishes this well.

Dan
Erwin Schwarz

Posts: 11
Registered: 2/26/18
Re: How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 28, 2018 10:59 PM   in response to: Dan Barclay in response to: Dan Barclay
Dan Barclay wrote:
It sounds like you may be trying to use a regular Delphi timer.

Etian's response refers to an IW timer (TIWTimer) which you would place on a form. It creates timer events (either sync or async) which will execute based on messages to the server.

Maybe it would help to know what you're using the timer for. It might help in coming up with an option that will work for you.

I'd suggest, in general, that the server just respond to session messages rather than using much internally generated operations. The TIWTimer accomplishes this well.

Dan

Thank you for your answers Etian and Dan. I'm using a regular Delphi TTimer in a DataModule because I read somewhere that the TIWTimer should be use inside a TIWForm and I don't want the Timer to run only when a web page is displayed, but all the time that the service is installed (even if an user session has not begin).

What I'm trying to achieve is a service that runs periodically, doing some database maintenance. I know a regular windows service can do the work, but as I also need an interface to interact with the DB, I want to use just 1 program to achieve both things. I know the IW application is running because I can go to the browser and I'll get the page display correctly.
Scott Gast

Posts: 82
Registered: 4/2/04
Re: How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 29, 2018 5:16 AM   in response to: Erwin Schwarz in response to: Erwin Schwarz
How often should the timer fire?
Could your database maintenance application run as a scheduled task?
Erwin Schwarz

Posts: 11
Registered: 2/26/18
Re: How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 29, 2018 7:05 AM   in response to: Scott Gast in response to: Scott Gast
Scott Gast wrote:
How often should the timer fire?
Could your database maintenance application run as a scheduled task?

Every minute, but I want to implement both task in just 1 program (db maintenance and browser).
Dan Barclay

Posts: 889
Registered: 11/9/03
Re: How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 29, 2018 10:51 AM   in response to: Erwin Schwarz in response to: Erwin Schwarz
Erwin Schwarz wrote:
Scott Gast wrote:
How often should the timer fire?
Could your database maintenance application run as a scheduled task?

Every minute, but I want to implement both task in just 1 program (db maintenance and browser).

Of course the IWTimer won't work for that, it has to live on a IWForm as you know.

I'm not sure how to make a timer work, I haven't needed it so haven't tried it.

However, I've seen some code using a loop/sleep in a thread. I just can't remember where I saw that. Maybe in the IWDemos somewhere. I've slept since then so have forgot, but if I recall it included a thread with a loop (with sleep) to execute the code. I can't remember if it continued the thread, or ended and relaunched, and can't remember how it was terminated. I just recall the concept.

Dan
Mark Johnson

Posts: 10
Registered: 3/16/00
Re: How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 29, 2018 6:14 AM   in response to: Erwin Schwarz in response to: Erwin Schwarz
Erwin Schwarz wrote:
Hi, I have tried countless ways to implement a timer routine inside a IW application without any luck (at the ServerController, inside a DataModule created by the ServerController or created directly at the main program, etc). The thing is that when running inside Delphi everything works great, but when I install it as a windows service, the timer won't work... if I open a browser, the page will display correctly (so I know the windows service is working), but the timer don't work. As I told you, I tried using a DataModule, so I wrote a .log file to verify what was happening: when I run the .exe file (with the /gui parameter) all works fine, but when I install it as a service the timer won't work and the log file shows me that the DataModule was destroyed. Any ideas on how to do this? or maybe it is not possible to implement a timer using IntraWeb as a windows service? Thank you!

Edited by: Erwin Schwarz on Jan 28, 2018 5:12 PM

Hi Erwin,

I've successfully used a TTimer on a ServerController when installed as a windows service. Maybe a permissions issue as to what account you used when installing as a service?

Regards,
Mark

Erwin Schwarz

Posts: 11
Registered: 2/26/18
Re: How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 29, 2018 7:09 AM   in response to: Mark Johnson in response to: Mark Johnson
Mark Johnson wrote:
Erwin Schwarz wrote:
Hi, I have tried countless ways to implement a timer routine inside a IW application without any luck (at the ServerController, inside a DataModule created by the ServerController or created directly at the main program, etc). The thing is that when running inside Delphi everything works great, but when I install it as a windows service, the timer won't work... if I open a browser, the page will display correctly (so I know the windows service is working), but the timer don't work. As I told you, I tried using a DataModule, so I wrote a .log file to verify what was happening: when I run the .exe file (with the /gui parameter) all works fine, but when I install it as a service the timer won't work and the log file shows me that the DataModule was destroyed. Any ideas on how to do this? or maybe it is not possible to implement a timer using IntraWeb as a windows service? Thank you!

Edited by: Erwin Schwarz on Jan 28, 2018 5:12 PM

Hi Erwin,

I've successfully used a TTimer on a ServerController when installed as a windows service. Maybe a permissions issue as to what account you used when installing as a service?

Regards,
Mark


I already tried a TTimer on the ServerController, but the ServerController is created when I installed the program as a service and is destroyed immediately. Only when I run it as a program the ServerController is not destroyed.
Alexandre Machado

Posts: 1,754
Registered: 8/10/13
Re: How can I implement a Timer?
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 29, 2018 10:56 AM   in response to: Erwin Schwarz in response to: Erwin Schwarz
I already tried a TTimer on the ServerController, but the ServerController is created when I installed the program as a service and is destroyed immediately. Only when I run it as a program the ServerController is not destroyed.

When you "install" it as a service it will be created and destroyed, because that's how service installation on Windows works (the application executes normally and terminates immediately). It doesn't mean that it will happen when the application is actually installed and working.

Don't use a Timer. Use a thread. IW already has a base thread for that:


uses IW.Common.Threads;
 
  TMyTimedThread = class(TIWTimedThread)
  protected
    procedure DoExecute; override;
  public
    constructor Create(const AName: string; AInterval: Integer); override;
    destructor Destroy; override;
  end;
 
{ TMyTimedThread }
 
constructor TMyTimedThread.Create(const AName: string; AInterval: Integer);
begin
  inherited;
  // initialize your stuff here. Anything that is private to your thread
end;
 
destructor TMyTimedThread.Destroy;
begin
  // release anything created on Create herer
  inherited;
end;
 
procedure TMyTimedThread.DoExecute;
begin
   // do your work here
   // this method will execute in your thread context  once at each AInterval milliseconds
end;
 


Then use it like this:

var
  MyThread: TMyTimedThread;
begin
  MyThread := TMyTimedThread.Create('My Thread Name here', 10000); // will execute at 10 seconds interval
  MyThred.Start;  // (or Resume depending on your Delphi version)
end;
 
On ServerController.OnDestroy, call TMyTimedThead.Finish so you will signal the thread that it is terminated.
 

Kind regards

Erwin Schwarz

Posts: 11
Registered: 2/26/18
Re: How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 29, 2018 11:57 AM   in response to: Alexandre Machado in response to: Alexandre Machado
Alexandre Machado wrote:
I already tried a TTimer on the ServerController, but the ServerController is created when I installed the program as a service and is destroyed immediately. Only when I run it as a program the ServerController is not destroyed.

When you "install" it as a service it will be created and destroyed, because that's how service installation on Windows works (the application executes normally and terminates immediately). It doesn't mean that it will happen when the application is actually installed and working.

Don't use a Timer. Use a thread. IW already has a base thread for that:

 
uses IW.Common.Threads;
 
  TMyTimedThread = class(TIWTimedThread)
  protected
    procedure DoExecute; override;
  public
    constructor Create(const AName: string; AInterval: Integer); override;
    destructor Destroy; override;
  end;
 
{ TMyTimedThread }
 
constructor TMyTimedThread.Create(const AName: string; AInterval: Integer);
begin
  inherited;
  // initialize your stuff here. Anything that is private to your thread
end;
 
destructor TMyTimedThread.Destroy;
begin
  // release anything created on Create herer
  inherited;
end;
 
procedure TMyTimedThread.DoExecute;
begin
   // do your work here
   // this method will execute in your thread context  once at each AInterval milliseconds
end;
 


Then use it like this:

var
  MyThread: TMyTimedThread;
begin
  MyThread := TMyTimedThread.Create('My Thread Name here', 10000); // will execute at 10 seconds interval
  MyThred.Start;  // (or Resume depending on your Delphi version)
end;
 
On ServerController.OnDestroy, call TMyTimedThead.Finish so you will signal the thread that it is terminated.
 

Kind regards


Thank you so much for the information, I'll try it and let you know how it goes!
Dan Barclay

Posts: 889
Registered: 11/9/03
Re: How can I implement a Timer?
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 29, 2018 12:07 PM   in response to: Erwin Schwarz in response to: Erwin Schwarz
Erwin Schwarz wrote:

Thank you so much for the information, I'll try it and let you know how it goes!

Alex's answer is absolutely the correct answer for IW, but if you want to use a timer function in other service applications I did find a number of hits of the strategy I described with a google search. Search on "Delphi timer in service".

Fortunately TIWTimedThread hides us from all that!

Dan
Erwin Schwarz

Posts: 11
Registered: 2/26/18
Re: How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 29, 2018 12:43 PM   in response to: Dan Barclay in response to: Dan Barclay
Dan Barclay wrote:
Erwin Schwarz wrote:

Thank you so much for the information, I'll try it and let you know how it goes!

Alex's answer is absolutely the correct answer for IW, but if you want to use a timer function in other service applications I did find a number of hits of the strategy I described with a google search. Search on "Delphi timer in service".

Fortunately TIWTimedThread hides us from all that!

Dan

Where should I implement the thread code? At the ServerController, on a new Datamodule, at the source code of the project? Thanks!
Alexandre Machado

Posts: 1,754
Registered: 8/10/13
Re: How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 29, 2018 12:50 PM   in response to: Erwin Schwarz in response to: Erwin Schwarz
Erwin Schwarz wrote:
Dan Barclay wrote:
Erwin Schwarz wrote:

Thank you so much for the information, I'll try it and let you know how it goes!

Alex's answer is absolutely the correct answer for IW, but if you want to use a timer function in other service applications I did find a number of hits of the strategy I described with a google search. Search on "Delphi timer in service".

Fortunately TIWTimedThread hides us from all that!

Dan

Where should I implement the thread code? At the ServerController, on a new Datamodule, at the source code of the project? Thanks!

You don't need a DataModule for it. Create a unit, put your new class in it. Reference it (create/destroy) from within ServerController. If this is required during the whole application life cycle, create it on ServerController.OnCreate and free it on ServerController.OnDestroy. Just have in mind that ServerController is initialized before any user session, so you shouldn't use object that haven't been created yet.
Erwin Schwarz

Posts: 11
Registered: 2/26/18
Re: How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 29, 2018 5:33 PM   in response to: Alexandre Machado in response to: Alexandre Machado
Alexandre Machado wrote:
Erwin Schwarz wrote:
Dan Barclay wrote:
Erwin Schwarz wrote:

Thank you so much for the information, I'll try it and let you know how it goes!

Alex's answer is absolutely the correct answer for IW, but if you want to use a timer function in other service applications I did find a number of hits of the strategy I described with a google search. Search on "Delphi timer in service".

Fortunately TIWTimedThread hides us from all that!

Dan

Where should I implement the thread code? At the ServerController, on a new Datamodule, at the source code of the project? Thanks!

You don't need a DataModule for it. Create a unit, put your new class in it. Reference it (create/destroy) from within ServerController. If this is required during the whole application life cycle, create it on ServerController.OnCreate and free it on ServerController.OnDestroy. Just have in mind that ServerController is initialized before any user session, so you shouldn't use object that haven't been created yet.

I'm sorry to inform you that it's not working again in this new implementation: inside Delphi, the thread is created, it executes every X seconds and destroyed when I close the application; installed as a service it won't work :( because the thread is destroyed just after creation at the ServerController.OnDestroy
Dan Barclay

Posts: 889
Registered: 11/9/03
Re: How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 30, 2018 9:41 AM   in response to: Erwin Schwarz in response to: Erwin Schwarz
Erwin Schwarz wrote:
Alexandre Machado wrote:
Erwin Schwarz wrote:
Dan Barclay wrote:
Erwin Schwarz wrote:

Thank you so much for the information, I'll try it and let you know how it goes!

Alex's answer is absolutely the correct answer for IW, but if you want to use a timer function in other service applications I did find a number of hits of the strategy I described with a google search. Search on "Delphi timer in service".

Fortunately TIWTimedThread hides us from all that!

Dan

Where should I implement the thread code? At the ServerController, on a new Datamodule, at the source code of the project? Thanks!

You don't need a DataModule for it. Create a unit, put your new class in it. Reference it (create/destroy) from within ServerController. If this is required during the whole application life cycle, create it on ServerController.OnCreate and free it on ServerController.OnDestroy. Just have in mind that ServerController is initialized before any user session, so you shouldn't use object that haven't been created yet.

I'm sorry to inform you that it's not working again in this new implementation: inside Delphi, the thread is created, it executes every X seconds and destroyed when I close the application; installed as a service it won't work :( because the thread is destroyed just after creation at the ServerController.OnDestroy

TMyServerController.IWServerControllerBaseDestroy() should not execute until you shut the IW server down. From your comment I understand that you are seeing it execute sooner?

Or, maybe you are declaring the timer variable in the procedure? It should be declared in the implementation section of TMyServerController.

Dan
Erwin Schwarz

Posts: 11
Registered: 2/26/18
Re: How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 30, 2018 10:27 AM   in response to: Dan Barclay in response to: Dan Barclay
Dan Barclay wrote:
Erwin Schwarz wrote:
Alexandre Machado wrote:
Erwin Schwarz wrote:
Dan Barclay wrote:
Erwin Schwarz wrote:

Thank you so much for the information, I'll try it and let you know how it goes!

Alex's answer is absolutely the correct answer for IW, but if you want to use a timer function in other service applications I did find a number of hits of the strategy I described with a google search. Search on "Delphi timer in service".

Fortunately TIWTimedThread hides us from all that!

Dan

Where should I implement the thread code? At the ServerController, on a new Datamodule, at the source code of the project? Thanks!

You don't need a DataModule for it. Create a unit, put your new class in it. Reference it (create/destroy) from within ServerController. If this is required during the whole application life cycle, create it on ServerController.OnCreate and free it on ServerController.OnDestroy. Just have in mind that ServerController is initialized before any user session, so you shouldn't use object that haven't been created yet.

I'm sorry to inform you that it's not working again in this new implementation: inside Delphi, the thread is created, it executes every X seconds and destroyed when I close the application; installed as a service it won't work :( because the thread is destroyed just after creation at the ServerController.OnDestroy

TMyServerController.IWServerControllerBaseDestroy() should not execute until you shut the IW server down. From your comment I understand that you are seeing it execute sooner?

Or, maybe you are declaring the timer variable in the procedure? It should be declared in the implementation section of TMyServerController.

Dan

What I see is that when I /install the service, both ServerController.OnCreate and ServerController.OnDestroy run, so the Thread is created and finalized immediately. I can see the same behavior when I /uninstall the service. On the other hand, when I run the program using /gui to show the embedded web server, everything works perfect. Here is the calls to the Thread Unit:

var
MyThread: TMyTimedThread;

procedure TIWServerController.IWServerControllerBaseCreate(Sender: TObject);
begin
MyThread := TMyTimedThread.Create('My Thread Name here', 10000);
MyThread.Start; // (or Resume depending on your Delphi version)
end;

procedure TIWServerController.IWServerControllerBaseDestroy(Sender: TObject);
begin
MyThread.Finish;
end;

Thank you so much for all your ideas that may help me with this.

Dan Barclay

Posts: 889
Registered: 11/9/03
Re: How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 30, 2018 11:14 AM   in response to: Erwin Schwarz in response to: Erwin Schwarz
Erwin Schwarz wrote:


What I see is that when I /install the service, both ServerController.OnCreate and ServerController.OnDestroy run, so the Thread is created and finalized immediately. I can see the same behavior when I /uninstall the service. On the other hand, when I run the program using /gui to show the embedded web server, everything works perfect. Here is the calls to the Thread Unit:

var
MyThread: TMyTimedThread;

procedure TIWServerController.IWServerControllerBaseCreate(Sender: TObject);
begin
MyThread := TMyTimedThread.Create('My Thread Name here', 10000);
MyThread.Start; // (or Resume depending on your Delphi version)
end;

procedure TIWServerController.IWServerControllerBaseDestroy(Sender: TObject);
begin
MyThread.Finish;
end;

Thank you so much for all your ideas that may help me with this.

The thread may be finishing, but maybe not because the servercontroller Destroy was executed. Is other code that event executing?

What does TMyTimedThread look like? Note that you should provide DoExecute and not Execute.

Dan
Erwin Schwarz

Posts: 11
Registered: 2/26/18
Re: How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 31, 2018 2:25 PM   in response to: Dan Barclay in response to: Dan Barclay
Dan Barclay wrote:
Erwin Schwarz wrote:


What I see is that when I /install the service, both ServerController.OnCreate and ServerController.OnDestroy run, so the Thread is created and finalized immediately. I can see the same behavior when I /uninstall the service. On the other hand, when I run the program using /gui to show the embedded web server, everything works perfect. Here is the calls to the Thread Unit:

var
MyThread: TMyTimedThread;

procedure TIWServerController.IWServerControllerBaseCreate(Sender: TObject);
begin
MyThread := TMyTimedThread.Create('My Thread Name here', 10000);
MyThread.Start; // (or Resume depending on your Delphi version)
end;

procedure TIWServerController.IWServerControllerBaseDestroy(Sender: TObject);
begin
MyThread.Finish;
end;

Thank you so much for all your ideas that may help me with this.

The thread may be finishing, but maybe not because the servercontroller Destroy was executed. Is other code that event executing?

What does TMyTimedThread look like? Note that you should provide DoExecute and not Execute.

Dan

I create a text file with the name of the event that created it, to know if it ran or not. Here is the thread simple code:

constructor TMyTimedThread.Create(const AName: string; AInterval: Integer);
var
F: TextFile;
sArchivo: string;
begin
inherited;
sArchivo := 'Create.txt';
AssignFile(F, sArchivo);
Rewrite(F);
CloseFile(F);
end;

destructor TMyTimedThread.Destroy;
var
F: TextFile;
sArchivo: string;
begin
sArchivo := 'Destroy.txt';
AssignFile(F, sArchivo);
Rewrite(F);
CloseFile(F);

inherited;
end;

procedure TMyTimedThread.DoExecute;
var
F: TextFile;
sArchivo: string;
begin
sArchivo := 'DoExecute.txt';
AssignFile(F, sArchivo);
Rewrite(F);
CloseFile(F);
end;

When ran with the /GUI parameter, I can see the files for the Create, OnExecute and after I close the mini browser, I get the Destroy text file. When I use /install, I get immediately the Create and Destroy text files. That's how I know what's going on. Maybe it's not possible to have the timer running in an IntraWeb application installed as a service... I don't know. Thank you for all your support.
Dan Barclay

Posts: 889
Registered: 11/9/03
Re: How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 31, 2018 2:48 PM   in response to: Erwin Schwarz in response to: Erwin Schwarz
Erwin Schwarz wrote:

When ran with the /GUI parameter, I can see the files for the Create, OnExecute and after I close the mini browser, I get the Destroy text file. When I use /install, I get immediately the Create and Destroy text files. That's how I know what's going on. Maybe it's not possible to have the timer running in an IntraWeb application installed as a service... I don't know. Thank you for all your support.

Your understanding of function execution as a service was correct, mine was wrong as Alex explained.

However, I think you should be able to have a timer running. It's just a thread. The assumption on starting/finishing it just weren't correct. I don't have the answer but will ponder it some more.

I'm doing similar things, but already had a "monitor" process that hit the server from time to time to get status and such. When I want to do something at the "server" level (instead of one session) I've always queued off that monitor hit because it was already there. As a result, I just haven't needed an application level timer so am not familiar with it.

But, note above that I referenced a google search on using a timer in Delphi as a service. There are a number of hits on that subject, so somebody is doing it successfully outside IW. It should be easier in IW given the already built class.

Dan
Alexandre Machado

Posts: 1,754
Registered: 8/10/13
Re: How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 5, 2018 10:49 PM   in response to: Erwin Schwarz in response to: Erwin Schwarz
Maybe it's not possible to have the timer running in an IntraWeb application installed as a service... I don't know.

This is not a timer, but a thread which executes at predetermined intervals. For practical purposes, it will behave just like one.
Anyway, again, this is not a problem. Your thread will be created and destroyed when you install your service, yes, so what's the issue? Unless you are trying to wipe out the user HDD during the destruction of your thread, this shouldn't be a problem, ever.
Alexandre Machado

Posts: 1,754
Registered: 8/10/13
Re: How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 30, 2018 12:50 PM   in response to: Erwin Schwarz in response to: Erwin Schwarz
What I see is that when I /install the service, both ServerController.OnCreate and ServerController.OnDestroy run

Exactly. I've already mentioned it. When you are installing a service (using the /install command line switch) the application runs. That's how Windows install services. It will run and I'm failing to see what is the issue here.
You only /install a service ONCE and when it is installed it is not supposed to do anything useful besides registering itself.
Dan Barclay

Posts: 889
Registered: 11/9/03
Re: How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 30, 2018 9:23 PM   in response to: Alexandre Machado in response to: Alexandre Machado
Alexandre Machado wrote:
What I see is that when I /install the service, both ServerController.OnCreate and ServerController.OnDestroy run

Exactly. I've already mentioned it. When you are installing a service (using the /install command line switch) the application runs. That's how Windows install services. It will run and I'm failing to see what is the issue here.
You only /install a service ONCE and when it is installed it is not supposed to do anything useful besides registering itself.

Are you talking about Installing it or Starting it? I wasn't thinking it ran the ServerControllerBaseCreate when installing, I'd never looked into it and "assumed" wrong. I find that Create is executed both with Install and Start.

I'll need to put some logging in to the Destroy operation. I'd planned on some cleanup code for destroy (cleanup temp stuff etc), which works great in standalone. It looks like I may need to reconsider for service. I'm running fine as service, but may not be taking care of everything like I want. Hmmm...

Dan
Alexandre Machado

Posts: 1,754
Registered: 8/10/13
Re: How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 5, 2018 4:58 PM   in response to: Dan Barclay in response to: Dan Barclay
Hi Dan,

Are you talking about Installing it or Starting it? I wasn't thinking it ran the ServerControllerBaseCreate when installing, I'd never looked into it and "assumed" wrong. I find that Create is executed both with Install and Start.

Installing it. When /install command line switch is used.

I'll need to put some logging in to the Destroy operation. I'd planned on some cleanup code for destroy (cleanup temp stuff etc), which works great in standalone. It looks like I may need to reconsider for service. I'm running fine as service, but may not be taking care of everything like I want. Hmmm...

Maybe it is not necessary. What can your application clean that can't be cleaned when installing?
Dan Barclay

Posts: 889
Registered: 11/9/03
Re: How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 5, 2018 5:49 PM   in response to: Alexandre Machado in response to: Alexandre Machado
Alexandre Machado wrote:
Hi Dan,

Are you talking about Installing it or Starting it? I wasn't thinking it ran the ServerControllerBaseCreate when installing, I'd never looked into it and "assumed" wrong. I find that Create is executed both with Install and Start.

Installing it. When /install command line switch is used.

I'll need to put some logging in to the Destroy operation. I'd planned on some cleanup code for destroy (cleanup temp stuff etc), which works great in standalone. It looks like I may need to reconsider for service. I'm running fine as service, but may not be taking care of everything like I want. Hmmm...

Maybe it is not necessary. What can your application clean that can't be cleaned when installing?

I wasn't clear. I don't have a problem with it running "cleanup" code when it installs, but I want to make sure it runs when I shut the service down else it may leave droppings. I haven't got back to checking on that but have made a note of it.

I install/start with
xxxxx /install /silent
net start xxxx

It executes the startup code with both lines. I haven't checked the destroy operation for service.

Dan
Erwin Schwarz

Posts: 11
Registered: 2/26/18
Re: How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jan 30, 2018 7:41 AM   in response to: Dan Barclay in response to: Dan Barclay
Dan Barclay wrote:
Erwin Schwarz wrote:

Thank you so much for the information, I'll try it and let you know how it goes!

Alex's answer is absolutely the correct answer for IW, but if you want to use a timer function in other service applications I did find a number of hits of the strategy I described with a google search. Search on "Delphi timer in service".

Fortunately TIWTimedThread hides us from all that!

Dan

I implemented the unit with the TIWTimedThread but as I create it and destroy it using the ServerController OnCreate and OnDestroy events, it's exactly the same problem: the Thread will be created and destroy immediately when installed as a service. May the Create/Destroy should be place somewhere else? I tried to put it at the project code, with same results.
Alexandre Machado

Posts: 1,754
Registered: 8/10/13
Re: How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 5, 2018 4:54 PM   in response to: Erwin Schwarz in response to: Erwin Schwarz
I implemented the unit with the TIWTimedThread but as I create it and destroy it using the ServerController OnCreate and OnDestroy events, it's exactly the same problem: the Thread will be created and destroy immediately when installed as a service. May the Create/Destroy should be place somewhere else? I tried to put it at the project code, with same results.

Erwin,

I explained this twice: when you "install" a Windows service (using /install command line switch), the OS (Windows) actually executes the application completely. It actually runs!! It runs and then terminates in a fraction of a second, because that's how Services work. All Windows services behave like that and this is how it should be. This is not a problem. This is not something unexpected.

I'm still failing to understand why this is a problem for your application, and you still haven't explained why it is a problem for you.
Erwin Schwarz

Posts: 11
Registered: 2/26/18
Re: How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 6, 2018 8:53 AM   in response to: Alexandre Machado in response to: Alexandre Machado
Alexandre Machado wrote:
I implemented the unit with the TIWTimedThread but as I create it and destroy it using the ServerController OnCreate and OnDestroy events, it's exactly the same problem: the Thread will be created and destroy immediately when installed as a service. May the Create/Destroy should be place somewhere else? I tried to put it at the project code, with same results.

Erwin,

I explained this twice: when you "install" a Windows service (using /install command line switch), the OS (Windows) actually executes the application completely. It actually runs!! It runs and then terminates in a fraction of a second, because that's how Services work. All Windows services behave like that and this is how it should be. This is not a problem. This is not something unexpected.

I'm still failing to understand why this is a problem for your application, and you still haven't explained why it is a problem for you.

Sorry for the misunderstanding, let me explain it better:
a) Past: I already have an application that runs as a service, where a one minute timer do some DB housekeeping
b) Present: Now I need a page to display and update the information of the DB (to configure parameters for the housekeeping)
c) Future: I want to develop a new program that solves issues a) and b) using IntraWeb as a browser to look into the DB, but just 1 program to do both task.

This is why I want to implement a Timer (or thread) to run periodically. When installed as a service, my application creates and destroys the thread, but it's never created again, so the timer won't run every minute; but the browser still works (I can get my display screen in a browser). I want it to be just 1 application, to do this whole thing. Hope this clarifies it, thank you.
Alexandre Machado

Posts: 1,754
Registered: 8/10/13
Re: How can I implement a Timer?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 8, 2018 4:06 PM   in response to: Erwin Schwarz in response to: Erwin Schwarz
This is why I want to implement a Timer (or thread) to run periodically. When installed as a service, my application creates and destroys the thread, but it's never created again, so the timer won't run every minute; but the browser still works (I can get my display screen in a browser). I want it to be just 1 application, to do this whole thing. Hope this clarifies it, thank you.

Yes, but when you /install your application it is not supposed to start (Execute) the thread. This is correct.

However, after installing your service, if you just START it, the thread will be created, executed and later will be destroyed, when the service STOPS. Have you tried it? This is how other threads in IntraWeb work (e.g. the session cleanup thread).

Bottom line is: Forget about /install. Doesn't matter what the service is doing during installation. It will work as you expect when the service is running.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02