In order for this site to work correctly we need to store a small file (called a cookie) on your computer. Most every site in the world does this, however since the 25th of May 2011, by law we have to get your permission first. Please abandon the forum if you disagree.

Para que este foro funcione correctamente es necesario guardar un pequeño fichero (llamado cookie) en su ordenador. La mayoría de los sitios de Internet lo hacen, no obstante desde el 25 de Marzo de 2011 y por ley, necesitamos de su permiso con antelación. Abandone este foro si no está conforme.

WinService sample ....

Xailer professional forum in English
ChrisGillard
Posts: 384
Joined: Tue May 01, 2007 5:49 pm

WinService sample ....

Postby ChrisGillard » Tue Oct 10, 2017 11:27 am

Hi,

I am very interested in TWinService and so also WinService sample program. I have compiled and run the WinService sample and it gives me the four buttons which all seem to do what they should.

I have taken a copy of the folder and want to make it try to make the service actually do something by adding to the ServiceMain() fuction in start.prg.

I do something like this to understand what is happening .... I get logging from the first line of the Procedure, but no other logging or beep?

STATIC PROCEDURE ServiceMain( oSender )

LogFile( "ServiceMain(1)" + " : " + Time()) // THIS DOES WRITE TO LOGFILE ..... GOOD

DO WHILE oSender:nStatus == ssRUNNING
/*
[ENGLISH]
Here you can do anything ;-)
FYI: You should allow the loop to continue, leaving a short pause (i.e. 1 second) to avoid the system to lock

[SPANISH]
Aquí puede poner lo que necesite
NOTA: Siempre debe permitir que el bucle continue, dejando una pequeña pausa (p.ej. 1 segundo) para evitar bloquear el sistema
*/

LogDisplay( "ServiceMain(2) : In ssRunning loop" ) // I DO NOT SEE THIS LINE

Beep( 2000 ) // I DO NOT HEAR THIS BEEP

ProcessMessages()

ENDDO

LogFile( "ServiceMain(3)" + " : " + Time()) // I DO NOT SEE THIS LINE

RETURN

It is quite possible that I am not understanding something!!

Any help would be appreciated.

Thank you.

Chris
ChrisGillard
Posts: 384
Joined: Tue May 01, 2007 5:49 pm

Re: WinService sample ....

Postby ChrisGillard » Wed Oct 11, 2017 1:01 pm

I have it working and I think I know the problem....

My problem was that from within the service it appears that certain things cant be done including interacting with the screen ....

LogDisplay() and Beep() certainly dont appear to work.

I had read that services should not interact with the screen but I had hoped that simple things like LogDisplay would work.

LogFile IS WORKING from within my service now.

< Note to Xailer team :

It would be useful if the WinService sample actually did something useful like some LogFiles() and a TTimer to tick away writing some LogFile() entries. Something very simple.

It might also be good to have a note in the Help file of a few hints and tips of what can and cant be done in a service.

Just a thought.

Regards

Chris
User avatar
jfgimenez
Site Admin
Posts: 5620
Joined: Mon Apr 06, 2015 8:48 pm
Contact:

Re: WinService sample ....

Postby jfgimenez » Wed Oct 11, 2017 7:18 pm

Chris,

sorry for the delay.

You're right: a service program cannot interact to the user in any way. It's a windows issue. A services can only do tasks in background, like listen to a TCP port, watch for file changes, run tasks on timer, etc.

LogFile() is working, since it only writes strings to a file. But LogDisplay() doesn't work because it shows a window. That's all.

BTW, when a service is started, its running directory is [windows\system32], not the one where the .exe resides. So, it's good to change it by using DirChange() before doing any task.
José F. Giménez
[Equipo de Xailer / Xailer team]
http://www.xailer.com
http://www.xailer.info
ChrisGillard
Posts: 384
Joined: Tue May 01, 2007 5:49 pm

Re: WinService sample ....

Postby ChrisGillard » Wed Oct 11, 2017 11:42 pm

Hi Jose,

Thanks very much for jumping in here.

Thank you for the tip of changing directories.

My project is to make an existing Xailer 'email SMTP sending server' which monitors a folder for new files that contain all of an emails details of To, From, Subject, Body etc and creates an email and sends it using a SMTP library called Mailbee. This is working fine as a regular application at the moment but of course needs the Windows server it is running on to be logged in with a user.

By converting the program to run as a service it will work after the Windows server is rebooted and not logged in.

This will mean less phone calls from the users company saying 'our emails are not going out' !!

So this new feature of Xailer will be very useful.

Thank you and keep up the good work.

Chris
User avatar
jfgimenez
Site Admin
Posts: 5620
Joined: Mon Apr 06, 2015 8:48 pm
Contact:

Re: WinService sample ....

Postby jfgimenez » Thu Oct 12, 2017 11:28 pm

Chris,

it's a very good sample of what should be run as a service. I'm sure you will avoid problems from now on :-D

I suppose you're using a timer to check if a new file is available, aren't you? If so, I can help you here: I have a routine to monitor changes in a directory. That is, your program is notified when a change happen in a given directory, instead of checking it actively. I'm using it for a few months successfull. If you're interested I could prepare and send it to you this next weekend.
José F. Giménez
[Equipo de Xailer / Xailer team]
http://www.xailer.com
http://www.xailer.info
ChrisGillard
Posts: 384
Joined: Tue May 01, 2007 5:49 pm

Re: WinService sample ....

Postby ChrisGillard » Sun Oct 15, 2017 10:34 pm

Hi Jose,

Yes, I have several applications that monitor folders for files .... so your technique would be very useful to me.

Thank you.

At the moment I am just trying to do a simple example at the moment bit It is not working for me at all? .....

I am just trying to use a timer to write a Logfile() line on every Timer execution.

Here is my simple code, which gives no error but no LogFile line.

Clearly I am not doing it correctly!

////////////////////////////////////////////////////////////
STATIC PROCEDURE ServiceMain( oSender )

local oTimer := TTimer():New( oSender )

oTimer:nInterval := 2000
oTimer:onTimer := { | | LogFile( "******************************* Timer event" + " : " + Time() ) }

DirChange( Application:cDirectory )

LogFile( "ServiceMain(11)" + " : " + Time())

DO WHILE oSender:nStatus == ssRUNNING

if ! oTimer:lEnabled
oTimer:lEnabled := TRUE
endif

ProcessMessages()

ENDDO

oTimer:lEnabled := FALSE

LogFile( "ServiceMain(15) stopped" )

RETURN
///////////////////////////////////////////////////////////////

Your help with this would be very usefful.

Thank you

Chris
ChrisGillard
Posts: 384
Joined: Tue May 01, 2007 5:49 pm

Re: WinService sample .... I have a TIMER THAT works!

Postby ChrisGillard » Tue Oct 17, 2017 11:53 am

Hi,

Basically I have a timer that works.

1. I did not have a CREATE() method on my 'created in code' Timer

2. I am not sure what oPARENT to use for the Timer? Sender does not work, I have left it blank and it works.

I am happy with this for the moment and I can continue with this.

Your 'folder change notification' idea would be very useful at some stage

A thought from me : I think it would be good if the Sample code included something like this timer to show people how to do something real with their service.

Thank you for your help so far.

Chris

This is my code that works for me...

STATIC PROCEDURE ServiceMain( oSender )

local oTimer := TTimer():New()
oTimer:Create() // oParent??
oTimer:nInterval := 1000
oTimer:onTimer := { | | LogFile( "******************************* oTimer event" + " : " + Time() ) }

DirChange( Application:cDirectory )

LogFile( "ServiceMain(10)" + " : " + Time())

DO WHILE oSender:nStatus == ssRUNNING

LogFile( "ServiceMain(12) In Do While" + " : " + Time())

if ! oTimer:lEnabled
oTimer:Enable()
endif

ProcessMessages( 1000 )

ENDDO

oTimer:lEnabled := FALSE
LogFile( "ServiceMain(15) stopped" )

RETURN

Return to “English”