Help with simple code example

This forum is designated to discuss SiComponents Scheduling Agent.
Post Reply
mkinsel
Posts: 8
Joined: Sun Jan 25, 2004 1:15 am

Help with simple code example

Post by mkinsel »

I am a new registered user and bought the component for a simple task. I don't need a scheduling wizard, but want to be able to create (and later on, edit) a task that calls an external .EXE file once a week in the middle of the night (i.e. 1:00 AM) from within my Delphi app. Can someone send me a simple example of the above code? Thanks for the help ...

Mark
isiticov
Site Admin
Posts: 2383
Joined: Thu Nov 21, 2002 3:17 pm

Post by isiticov »

The requested demo is available at:
http://www.sicomponents.com/soft/TaskSc ... unDemo.zip
mkinsel
Posts: 8
Joined: Sun Jan 25, 2004 1:15 am

Thanks for the help!

Post by mkinsel »

Thanks for the demo. I got my code built and it works great!

Mark
mkinsel
Posts: 8
Joined: Sun Jan 25, 2004 1:15 am

Example application

Post by mkinsel »

The example application compiles file, but the Windows scheduler won't run it. It gives the status as "Could not start". I have tried with two applications of my own using your example and they have the same problem. One other point, in my applications, I want the scheduler to restart the calling application at the designated time.
isiticov
Site Admin
Posts: 2383
Joined: Thu Nov 21, 2002 3:17 pm

Post by isiticov »

There could be one of the following reasons for this:
1. You've specified wrong account name and password
2. It is impossible to find the path to executable. In this example there is used TaskItem.ApplicationName := Application.ExeName; Please check that ApplicationName contains the FULL path to executable.

Hope this helps.
mkinsel
Posts: 8
Joined: Sun Jan 25, 2004 1:15 am

Simple example

Post by mkinsel »

What account name and password should I use? The account and password for logging onto the computer at start up? There isn't one on this machine.

Mark
basil
Posts: 2
Joined: Mon Aug 02, 2004 6:18 am

Post by basil »

Hello,

yes, if you run the task on Windows NT/2k/XP, then we recommend to use AccountName and Password, that are used to logon to Windows.

On Windows 9x the task does not need an account and password at all, just make sure the ApplicationName is specified properly.

Best regards
mkinsel
Posts: 8
Joined: Sun Jan 25, 2004 1:15 am

Simple example

Post by mkinsel »

Is there an API / function call to get the Account Name and Password programmatically? It would make my life easier. I did do some testing and specifying the account name and password seemed to fix the problem. Thanks for the help ...

Mark
basil
Posts: 2
Joined: Mon Aug 02, 2004 6:18 am

Post by basil »

Hello,

The Windows API function "GetUserName" retrieves the name of the user currently logged onto the system. This function might simplify setting the task paramaters a little but the password anyway should be typed by the user.

Another way is running task under "SYSTEM" account. In this case BOTH AccountName and Password should be empty and the task should NOT have the flag tfRunOnlyIfLoggedOn. Example:
...
if (Trim(Accnt) = EmptyStr) and (Trim(Pwd) = EmptyStr) then
FTask.Flags := FTask.Flags - [tfRunOnlyIfLoggedOn]
else
FTask.Flags := FTask.Flags + [tfRunOnlyIfLoggedOn];
FTask.SetAccountInformation(Trim(Accnt), Trim(Pwd));
...
Note, scheduling tasks under the "SYSTEM" account has some limitations. For example, a task running under SYSTEM does not allow interaction with the logged user (that is no message boxes, prompts, any dialogs).
mkinsel
Posts: 8
Joined: Sun Jan 25, 2004 1:15 am

System account

Post by mkinsel »

I'm still having problems getting my project to work. I am making an application that automatically sends a file as an attachment to an e-mail address. I want this file to be scheduled to be sent once a week. I have tried using the System account without any luck. When I manually try to use the system account in the XP task scheduler, it says my program is running with nothing showing on the screen or being sent. Unfortunately, I can't use the administrator account in Window XP since many people (including myself) don't remember the password they use with their login. Norton Antivirus seems to have found a way to use the administrator account without the user providing a password. Any suggestions on how to fix my problem?

Mark
isiticov
Site Admin
Posts: 2383
Joined: Thu Nov 21, 2002 3:17 pm

Post by isiticov »

There are 2 options:
1) use the SYSTEM account (empty accountname and password). In this case however, the application indeed does not show any dialogs. Therefore you will need to redesign your applcaition so it does not require any interaction with user.
2) You can use any account or create a special account for this task, but set the flag

Code: Select all

FTask.Flags := FTask.Flags - [tfRunOnlyIfLoggedOn];
Then the task will run when any user is logged on. However, if the task's account is different from the current user, the last one still won't be able to interact with program.
P.S. You tried to run a task under system account in the XP task scheduler and the email was not sent. It is possible that Norton Internet Secutity firewall just didn't allow the email sending.
mkinsel
Posts: 8
Joined: Sun Jan 25, 2004 1:15 am

System account

Post by mkinsel »

I got my application running now with the System account; however, I have a problem with my button to edit the schedule using the TTaskPropertiesDialog component. My problem is that the changes made using the TTaskPropertiesDialog are not being saved / applied to the Windows Task Scheduler.

My onClick code for the Edit Schedule button is:

TaskDialog1.TaskName := 'MyName';
TaskDialog1.Execute;

Is the Execute method supposed to save the changes? What do I need to do differently?
mkinsel
Posts: 8
Joined: Sun Jan 25, 2004 1:15 am

System account

Post by mkinsel »

Is there a way to set the Run As property / field in the Windows XP Task Scheduler programmatically with your components. To make the system account work, you need to specify the Run As field as "System".
isiticov
Site Admin
Posts: 2383
Joined: Thu Nov 21, 2002 3:17 pm

Post by isiticov »

Just add a call:
Task.Deactivate(True);

after

TaskDialog1.Execute;


Run As could be performed using SetAccountInformation() method.
Post Reply