FoxPro Programming
Running VFP application as a windows service
Gravatar is a globally recognized avatar based on your email address. Running VFP application as a windows service
  Bob
  All
  May 1, 2020 @ 08:21am

I am trying to create a VFP timer application that I can run as a Windows service I have tried just a plain old VFP.exe but it doesn't fire. Then I tried setting up a C# program with timer to call the VFP program, but it didn't want to fire either. Does anyone have a suggestion as to how I might be able to set something like this up to run as a windows service? Thank you for any help you can provide.

Gravatar is a globally recognized avatar based on your email address. re: Running VFP application as a windows service
  Rick Strahl
  Bob
  May 1, 2020 @ 03:50pm

What are you doing in your EXE? If you're running a Service the service has to keep running and if you build a 'console' application (without Fox UI) your code will run and exit and never wait.

I think you you need to make sure you have a READ EVENTS loop to keep the applicaition alive in order for the timer to have something to do come back to.

FWIW, that's pretty much what Web Connection does - Web Connection, which is a server application (in File Mode), starts up the executable, and then sits on READ EVENTS until the server is either terminated (if the Server Windows is visible when run as Interactive User) or a kill command is sent to it.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Running VFP application as a windows service
  Bob
  Rick Strahl
  May 1, 2020 @ 04:07pm

Every 1/2 hour I want it to send a text file to a remote server to send out appointment reminders. Every 5 minutes I want to check a folder on another server to see if there are any returned text messages to post. On some systems it runs a backup job once a day. I am doing this now by using a timer control from the main form on the local or Azure server computer. I have to depend on the users not to exit the program in order to keep it running. I guess I could create another exe with a hidden form that basically only contains a timer object. Then set it to run when the computer starts. This would make sure it keeps running in case of a reboot.

Gravatar is a globally recognized avatar based on your email address. re: Running VFP application as a windows service
  Rick Strahl
  Bob
  May 1, 2020 @ 05:28pm

Sorry I meant what is your code doing to run this? How are you running a 'service'? If you're not doing what I describe with READ EVENTS or running some sort of form, then your app is probably just shutting down immediately.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Running VFP application as a windows service
  Michael Hogan (Ideate Hosting)
  Bob
  May 25, 2020 @ 08:32am

Bob;

You might want to take a look at AlwaysUp https://www.coretechnologies.com/products/AlwaysUp/

Tuvia did a presentation on it awhile back and I've found it very helpful in scheduling applications. I have a couple of little exe's set to restart every xx minutes.

Let me know if you need specifics.

Gravatar is a globally recognized avatar based on your email address. re: Running VFP application as a windows service
  Jeff L
  Michael Hogan (Ideate Hosting)
  May 25, 2020 @ 10:30am

Call me simple and old fashioned, but I would use the Windows Task Scheduler for these jobs. I would also separate them into 3 discrete VFP EXEs rather than one EXE.

You can schedule a task to run every 5 minutes indefinitely. You can schedule a task to run once every hour or 30 minutes or once every day.

Task Scheduler is free. The VFP code is simple with no READ EVENTS involved (typically). I would add some logging on errors however. The Task Scheduler logs the details of each time it runs a task. Since the app fires and closes down, you probably don't have to be concerned with memory leaks.

Gravatar is a globally recognized avatar based on your email address. re: Running VFP application as a windows service
  Rick Strahl
  Jeff L
  May 25, 2020 @ 01:06pm

Task scheduler works great, but it's not the easiest thing to automate the creation of tasks. The problem is that if you manually create tasks you have to remember you have to install it - it's disconnected from your application. If in a few years you move the application it's likely you'll forget the scheduled task that goes with it.

There are APIs for it but they're not widely supported by installers and other tools.

Just took a quick look - it looks like there are ways at least to automate the creation tasks with .NET and some NuGet libraries, but creating a task requires admin rights, so this would have to be triggered from an installer to be realistic.

Creating Scheduled Tasks (SO)

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Running VFP application as a windows service
  Jeff L
  Rick Strahl
  May 25, 2020 @ 02:37pm

True dat about configuration of Task Scheduler. And you usually need to create a user to run the task(s) with and there is no UI.

Years ago, I wrote a mission critical VFP 6 app that used a timer and the problem was that the timer would fall asleep and not fire. Interesting was that if you took the mouse and nudged the app's form via the caption bar the timer would wake up. So, something about Windows and VFP. I had to write a watcher programmer and then you get into writing watchers for watchers.

The shop was FoxPro for Windows and FoxPro for DOS, so my Visual FoxPro app was new to the environment. So, it was very embarrassing when this app went to sleep. I added all sorts of code to try to keep it awake - nothing worked.

So, I'm of the once bitten, twice shy regarding VFP timers and have relied on Windows Task Scheduler instead.

Gravatar is a globally recognized avatar based on your email address. re: Running VFP application as a windows service
  Rick Strahl
  Jeff L
  May 25, 2020 @ 02:45pm

Interesting re-Timer. Web Connection uses timers in file mode running server applications and there are quite a few fairly busy sites running Web Connection in file mode. Timers in this case have never failed. Then again Web Connection is not UI intensive so there aren't a lot of other events firing, but nevertheless I think the time is pretty reliable other than perhaps missing a tick here or there. The main issue of what can happen is that hte timer goes out of scope and then you lose the timer so making sure the timer is global or attach to _screen or something like that (or in Web Connection to the persistent Server object) is key.

That said I'm not a fan of timer either and try to avoid as much as possible. I've also built solutions around .NET threads that run in the background and then call back - spin up a thread, let the thread check for conditions and if they are met fire a message into a FoxPro object that can then take action on the event. Same as a timer, but more reliable because the operation sits and waits externally.

That said Scheduled Tasks work great as long as you can find a good way to make sure they are created on the target machines and it doesn't become some side not 20 pages into the docs where it surely will be forgotten when the system gets reinstalled 5 years from now... ask me how I know 😃

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Running VFP application as a windows service
  Jeff L
  Rick Strahl
  May 25, 2020 @ 02:59pm

When I worked at MS in the VFP Testing dept in the VFP 3-6 era there was a manual test that had a form and a surface that you drove the mouse over and the mouse coordinates would appear in textboxes on the form. There was a timer involved and until you moved the mouse fast enough the timer would not fire. It was rather funny. Of course, I bugged it and it got fixed, but it makes me wonder how the VFP timers are tied together with the OS. Certainly, the mouse movement is an OS event.

And I understand that years down road on new hardware that if you forget to include those Task Scheduler tasks that you're gonna find out the hard way somehow.

© 1996-2020