Recent

Author Topic: Service application can't stop  (Read 9062 times)

MaartenJB

  • Full Member
  • ***
  • Posts: 112
Service application can't stop
« on: April 19, 2013, 12:51:51 pm »
Hi, I've made a little service application (lazdaemon). When I assign "DataModuleExecute", for example something simple as:

Code: [Select]
procedure T_Testing.DataModuleExecute(Sender: TCustomDaemon);
begin
    sleep(50);
end;

It is not possible to stop the service anymore. I must kill it from the taskmanager. When it's not assigned, it works fine.

What am I doing wrong here?

Thanks.

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: Service application can't stop
« Reply #1 on: April 19, 2013, 04:16:28 pm »
You're not supposed to quit the OnExecute procedure until the service is terminated.

See (second post): http://www.lazarus.freepascal.org/index.php/topic,7047.0.html

What are you trying to do exactly ?

MaartenJB

  • Full Member
  • ***
  • Posts: 112
Re: Service application can't stop
« Reply #2 on: April 22, 2013, 09:52:46 am »
Hi Chris,

Thanks for your reply. I've tried the code posted in that tophic aswell, but the problem remains the same, not being able to stop it.

I'm using Lazarus 1.0.8, fpc 2.6.2, win7 32b


ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: Service application can't stop
« Reply #3 on: April 22, 2013, 02:10:54 pm »

Well, I still don't know why you are trying to do, but I guess you want to test the execution part of a service/daemon.

Just making a few tests for my own, I've got the same kind of problems with lazdaemon. I can eventually force by program the termination of the execute loop, but it's still not completely correct; the un-installation is not immediate, for instance.

I'm not sure if there is a problem within the population of the Terminate event inside Lazdaemon, or if I've wrongly interpreted how to use it.

Anyway, there is -at least- 2 other working possibilities:


1/ Try the daemon sample program coming with FPC (i.e. don't use any more the lazdaemon package). This one is working properly.

The sample code is located in (if you are using FPC 2.6.2):
$(LazarusDir)\fpc\2.6.2\source\packages\fcl-extra\examples.

Open the daemon.pp as a Program project (i.e. don't use a Lazarus application with forms, etc...) and compile it.

To install it, type (console mode) : daemon -i (assuming your .exe produced file is daemon.exe)
To uninstall it, type (console mode) : daemon -u

You can manually start and/or stop it using the windows service manager (if you're using windows).

A service called 'Test daemon' is created. You can see all its debug outputs into the windows event viewers (Applications part).


2/ Use a timer inside lazdeamon (windows solution only), to periodically call your own program part.

See this topic: http://www.lazarus.freepascal.org/index.php/topic,20308.0.html

There are a few source code samples (attached files) using lazdaemon and a timer inside it.

MaartenJB

  • Full Member
  • ***
  • Posts: 112
Re: Service application can't stop
« Reply #4 on: April 22, 2013, 03:33:33 pm »
Hi Chris,

The example project you suggested is working fine. Thanks!

I'm using the execute part for monitoring network changes.

Thanks again!


MaartenJB

  • Full Member
  • ***
  • Posts: 112
Re: Service application can't stop
« Reply #5 on: April 24, 2013, 02:29:31 pm »
Hi Christ,

I noticed that when the thread is running, it causes a high cpu load, even when it's not doing anything. Do you know what this might be?

Thanks.

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: Service application can't stop
« Reply #6 on: April 24, 2013, 04:09:50 pm »
Yep. It seems there is definitively something wrong concerning the execute method: the "pause" seems not to be fully pausing at all.

I can only propose you to have -at least- a look at the topic I've suggested before, as another alternative. See Windows service with FPTimer: http://www.lazarus.freepascal.org/index.php/topic,20308.0.html

Look at the "TestSer.zip" attached file as a sample. This one is using the lazdeamon package, but it could be easily adapted to the simplier FPC deamon program.

As the execute method is not used in this sample, there is no high CPU usage at all.

If you don't want to bother with the log file produced (i.e. procedure "CallWriteLn" which is writing into the log file CLOG_FILENAME ='C:\TestSer\TestSer.Log'), you can replace the whole procedure with:
Code: [Select]
procedure CallWriteLn(Const SS: String);
begin
  Application.Log(etcustom,SS);
end;

**EDIT ** I forgot to say that this sample is only valid for Windows (I don't know which OS you are using).
« Last Edit: April 24, 2013, 04:11:45 pm by ChrisF »

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: Service application can't stop
« Reply #7 on: April 24, 2013, 07:14:26 pm »
I've finally found the origin of the high CPU usage.

The windows GUI loop for the service is responsible of it. I don't know if it has really been implemented on purposes or not, but anyway it's currently unusable IMHO.

It' theoretically possible to use your own GUI Loop (GUIMainLoop property for TCustomDaemonApplication class), but I've not been able to succeed into registering my own one (I always get a compilation error).

Anyway, it's still possible to change the by-default GUI loop into the FPC source code (at least for test purposes). This solution is working properly for me.

Unit: ($lazdir)\fpc\2.6.2\source\packages\fcl-extra\src\win\daemonapp.inc
Function: Function TCustomDaemonApplication.RunGUIloop(P : Pointer) : integer;
Patch :
Code: [Select]
      Repeat
        if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then
          begin
          if (Msg.Message<>WM_QUIT) and (Msg.Message<>WM_ENDSESSION) then
            begin
            TranslateMessage(Msg);
            DispatchMessage(Msg);
            end
          else
            Terminate;
          end;
        WaitMessage;                  //  <------------ Add this one
      Until Terminated;

You'll have to recompile then the "fcl-extra" package, and put the produced files into the correct unit directory.

I'm just going to report this issue. If there is really a good reason for the current lack of the 'waitmessage' API call, I guess I'll learn which one.

MaartenJB

  • Full Member
  • ***
  • Posts: 112
Re: Service application can't stop
« Reply #8 on: April 25, 2013, 11:03:55 am »
Hi Chris,

That solved the cpu issue, the only thing is when you stop the service, the execute is not closed right away. It takes 30 sec for it to be removed from the processlist.

With a little addition on your code it works fine
Code: [Select]
        if not Terminated then WaitMessage;                  //  <------------ Add this one

Thanks again Chris for your time!

 

TinyPortal © 2005-2018