Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timer doesn't trigger when run under Windows Task Scheduler

Rajesh Karunakaran

Programmer
Sep 29, 2016
557
MU
In my application with UI, I have a timer. It checks, if the hour of time is 9, 10, 11... and so on and if matches, ie at each hour, process something and upload to an Ftp.
If I run this in pc as usual the UI displays and the timer correctly does the process at each hour.

When I set a Task in Windows Task Scheduler, to start when the PC starts, it shows 'Running' and I can see it in the Task Manager. However, it doesn't do the process and upload.

Earlier, I had the Timer1.Enabled = .T. in the form's Activate. Then I shifted it to the Init method thinking maybe in Task Scheduler, it doesnt get activated as there's no UI. But still, it doesn't run the process code.

The Timer is added onto the form by control drag/drop. Should I add it programmatically? Will that solve the problem?

Anyone has any idea?
 
How's Task scheduler configured? Usually task scheduler does not start an EXE immediately, but by a time schedule, and if that is set to full hours, the exe will start shortly after full hours. Or do you only use it to autostart your exe once at every Windows startup? It ma only start after a Windows login.

I wonder about your statement it runs without UI. Without you also have no form init, do you?

You can easily find out what actually happens in the exe with SET COVERAGE.
 
Hi Chris,

Yes, I meant, when the app runs through Task Scheduler, it doesn't show UI.
In that case, as you said, no form Init as well!

So, I think, I need to add the Timer as a public control in my startup PRG file, not inside the form.
Let me check that.

Will come back here.
 
If you scheduled the task to system start before a user logs in and you start a form then this might trigger an error and stops, right away. Regarding the Enabled property, that can be set .T. in a timer class at design already, so it starts the first timer event Interval milliseconds after its creation.

Edit: After testing, a simple VFP executable showing its _Screen won't trigger an error, even when the scheduled task is triggered by system start, hidden and before user logs in, where it starts in session 0. If you have that configuration (You didn't answer my question about how your scheduled task is configured) the only restriction is that you can't have access to mapped drives, as the drive mapping will only be done after login. So you can only access local drives, for example. If anything errors the timer stops working, of course. So establish an error handler logging errors to see what goes wrong.
 
Last edited:
Chriss,

This is solved.
I just need to enter the folder of my application under Actions -> Settings -> 'Start in (optional):'
Then it started, it creates my log file and now I am just waiting for it to trigger at the next Run Hour.
Will update the status here.
 
Indeed, if you don't specify "start in", the default start directory is C:\Windows\System32, not the EXE directory.

Dependency on EXE path should alsways be programmed by using SYS(16), or you set the start in directory for a scheduled task or in a shortcut link. But whenever a user double clicks an EXE the default start path is not necessarily that of the EXE, so I always have a routine determining the EXE path by JUSTPATH(SYS(16,0)). That way you don't depend on a start path at all, as you can CD or SET DEFAULT there.
 
Glad to see that you got it working. I wrote a custom timer class based on the Win32 API. See attached. The DeclareWinAPI.prg should be added as a library:

Code:
SET LIBRARY TO DeclareWinAPI.prg ADDITIVE

The class works pretty much the same way as the VFP control.
 

Attachments

  • apitimer.zip
    37.6 KB · Views: 7

Part and Inventory Search

Sponsor

Back
Top