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.

Gravatar is a globally recognized avatar based on your email address. re: Running VFP application as a windows service
  Rick Strahl
  Doug Hennig
  May 27, 2020 @ 12:30pm

Nice...

But I think regardless this still requires admin rights to create tasks.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Running VFP application as a windows service
  Doug Hennig
  Rick Strahl
  May 27, 2020 @ 12:43pm

Not admin rights, but you do have to specify the credentials of the user the task runs under and that user needs to have the correct permissions.

Doug

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

I thought the API (and certainly the powershell commands) require Admin rights to create tasks even if you create tasks that don't require elevated rights.

+++ Rick ---

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

Speaking of Power Shell commands and Task Scheduler, here's what I do with my WWWC, PS, and Task Scheduler.

I have a table that houses a collection of tasks and when they need to be run. Each record has WWWC url (a foxpro routine) and a date of when it needs to be processed. The routine generates a power shell file named runme.ps1 with the following.

$Browser=new-object -com internetexplorer.application
$Browser.navigate2("insert your read to go url here")
$Browser.navigate2("insert a second url here ")
$Browser.visible=$false

The Power Shell file is executed every 5 minutes from a single task that runs. You setup POWERSHELL with the following action

Program/Script: powershell Arguments: -noprofile -executionpolicy bypass -file C:\folder\runme.ps1

You can setup the frequency to be every 5 minutes or whenever you want and you can now control your task scheduler without any pesky manipulation of power shell directly. Use fox to call your site or others via http and on any schedule you wish.

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

There are dedicated powershell commands for HTTP access - you don't need to use internet explorer automation for this.

Powershell has a minimal version of curl built into to hit a URL.

curl "https://west-wind.com"

or you can use:

# returns an object
$response = Invoke-WebRequest -Uri "https://webconnection.west-wind.com"

# Content and Headers
$response.Content
$response.Headers
Gravatar is a globally recognized avatar based on your email address. re: Running VFP application as a windows service
  Doug Hennig
  Rick Strahl
  May 29, 2020 @ 07:32am

I just tried it logged in as a limited user account and was able to create and delete a scheduled task programmatically.

Doug

Gravatar is a globally recognized avatar based on your email address. re: Running VFP application as a windows service
  MarshalR
  Michael Hogan (Ideate Hosting)
  Nov 3, 2021 @ 04:48am

Will AlwaysUp run in Windows 7?

Gravatar is a globally recognized avatar based on your email address. re: Running VFP application as a windows service
  Michael Hogan (Ideate Hosting)
  MarshalR
  Nov 3, 2021 @ 07:53am

30 day free trial to find out!

Gravatar is a globally recognized avatar based on your email address. re: Running VFP application as a windows service
  MarshalR
  Michael Hogan (Ideate Hosting)
  Nov 3, 2021 @ 11:42am

Contacted the Vendor - they have an older version which will run on Windows 7. Do you use the product? Thanks for bringing to my attention Bob.

Marshal

Gravatar is a globally recognized avatar based on your email address. re: Running VFP application as a windows service
  Michael Hogan (Ideate Hosting)
  MarshalR
  Nov 3, 2021 @ 11:45am

Yes - I am using it for two client installations.

Tuvia did a presentation on it several years ago at SWFox and I've been using it ever since. Great product!

© 1996-2024