Welcome! Log In Create A New Profile

Advanced

[WD23] Creating a Service

Posted by JP 
JP
[WD23] Creating a Service
October 18, 2018 10:24AM
Hello All

I need to create a service app. I see in the help is says this in the introduction:

"If the purpose of the service is to run a recurring task, we advise you to develop a standard application and to start this application in a scheduled task."

Can anyone clarify what this is saying - I am unsure.

Thanks
Re: [WD23] Creating a Service
October 18, 2018 11:10AM
Develop your service app as a normal WD project.
It should contain only the functions, procedures and data access necessary to enable it to carry out its required purpose.
It will not have any windows or user interface.
Generate it as a exe as normal and call this from a command line in the scheduled task.

I have, as a simple example, one running 24/7 monitoring a folder which receives payment receipt records (text files) sent from Sage,
The functionality in the app validates the content, writes records into the DB and archives the document.
JP
Re: [WD23] Creating a Service
October 18, 2018 11:12AM
Hi Derek,

OK, my app is indeed a simple thing. Can you expand/clarify your sentence: "Generate it as a exe as normal and call this from a command line in the scheduled task." - I have not done this before so quite a newbie with this so any help/info welcome.

Cheers!
Re: [WD23] Creating a Service
October 18, 2018 12:39PM
Hi JP,

It says that if you only need to run the task once, then you don't need to create a service.
You can create a normal program without any GUI and use the Windows task scheduler to run it periodically.
That way the program only runs when needed and does not use any resources all the time.
It is easier to debug, because you can test it by running it manually.

Kind regards,
Piet
Peter Holemans
Re: [WD23] Creating a Service
October 18, 2018 01:32PM
Hi JP:

These are two different things:
- Scheduled task:
Uses the windows task scheduler (or cron in a unix/linux environment) to run a program at regular intervals (e.g. every monday at 12:00 or every day from 8 to 18 every 5 minutes). Typically these are server side programs that run batch jobs at certain times. You can implement it on an end-user windows PC as well but I have rarely seen it and it is a pain to auto install and maintain (elevated rights) and runs in the user session.

- Service:
This is a background process that runs constantly and even can run outside a user session (without anybody being logged on to the machine).
It is ideal to have push or pull processes polling or sending at certain intervals. In WX ServiceWait() determines how long a service will wait before the next iteration. If you define a service configuration in your WX project you will have additional events in the main project code to intervene in the service process. In general I prefer this over scheduled tasks for background processes that need to execute at repeating intervals within a day. One tip: don't use the crappy WX service installer to package and distribute your service entries.
Instead use basic SC commands to install and configure your service.
Also there is one thing that is a bit hard to implement if you have long running processes by your service and that is catching the service stop request (e.g. at shutdown or by a user via the services.msc interface). The trick is to slice your long running process up in smaller blocks in between you can catch the Service Stop request and end it in a fashionable manor without breaking e.g. transaction integrity. Depending on your requirements this may not be an issue at all though.

I hope this enlightens you a bit in the area...

Cheers,

Peter Holemans
JP
Re: [WD23] Creating a Service
October 18, 2018 02:33PM
Peter,

Thanks so much for this very useful info. Basically what I need to do is poll a internet accessible server for a file, download it, and process it. Probably once every 5 minutes. I also need this to run without a user session. The process is very quick and spends most of its time doing nothing. The program itself is very simple. So it seems a service is best option for me.

What I don't know (relating to your post) is: how to install this service for an end-user? What does "SC commands" refer to in your post? I need to be able to let users who have little IT skills to easily install (or uninstall) my app.

Much appreciate any further help.

Thanks
Peter Holemans
Re: [WD23] Creating a Service
October 18, 2018 03:00PM
Hi JP,

1) Define your WX configuration as a service
2) In the end this will create an exe of type service
3) If you have a double configuration (e.g. classic exe and service exe that you will generate) then in the main project code you can distinguish between the mode by using code like IF InServiceMode() THEN …
4) SC command is the command to manage services in Windows
E.g. to create a service (this is just a base sample) for your generated exe this is sufficient: sc create "My Service Name I like to give" binpath= "D:\Myfolder\myexename.exe".


This means if you call sc commands from your main program to for example to see if the service exists and if not install it via these commands or by calling a .cmd script that does all the service handling... If you're too lazy you can use the WX installer to install your service but I don't like it as it is a lot of overhead...

Cheers,

Peter
JP
Re: [WD23] Creating a Service
October 18, 2018 03:42PM
Peter,

Again thank you. So would you say a good model for this would be to create (1) an interface exe where a user can set various options and settings for the service and (2) the service exe itself. Then the interface exe could use the WinDev service functions to create the service entry using ServiceInstall(), start or stop the service using ServiceStart() and ServiceStop(), and uninstall the service if no longer needed using ServiceUninstall(). Does that sound like a good approach?

Thanks
Re: [WD23] Creating a Service
October 18, 2018 03:52PM
JP,

Windev also has "service" functions like ServiceInstall(...), but they basicly do the same as the SC commands of Windows.
Have a look at this example c:\WINDEV 22\Examples\Training\WD Service\ and try to follow what is happening. It will give you all the info you need.

The example first runs like a regular executable and check on the local system if the app is already installed "as a service". If not it gives you the option to do so, using the functions mentioned above. Be aware to start the executable with admin rights! (actually 'service install rights')). The same executable is then installed as a service.

One thing you have to keep in mind (but that count for a scheduled task as well) is to strip ALL Info(), Warning() and Error()'s or other Dialogs waiting for user input. They will cause you service/task to "hang".

I use a service myself for maiing tasks, syncing data with 3rd partys and some other tasks. Runs pretty well and stable.

Arie
JP
Re: [WD23] Creating a Service
October 18, 2018 04:01PM
Thank you Arie. My service app is very basic and simple - no user interface needed at all.

Cheers!
JP
Re: [WD23] Creating a Service
October 19, 2018 10:25AM
Hi Arie

For some reason I don't have these examples installed - using WD23. Anyway, I have written a test service project which just loops and creates some files on disk so I can see it working. I then have another exe that uses ServiceInstall() to install the service (or uninstall it). That seems to work OK and the service is created (or removed) in the services list.

But I cannot start the service - Windows says it started and then stopped straight away and my test loop code does not run. I am not sure what user I should be using for the service variable? I need my service to have admin rights on the local machine.

Any suggestions appreciated !
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: