This project has moved. For the latest updates, please go here.

Getting error when RegisterTaskDefinition again

Topics: Errors
Apr 24, 2014 at 12:25 PM
Hello,

I am using the task scheduler to start a little program which do some database work for me.
A service starts this task with an timer. If the timer is lower than the old, the newer will be used.
When I start the programm it works fine. When the service called it first it works fine. When the service called again it crashs with the following error.

The only thing I get is this:
(32,7):Command:

Microsoft.Win32.TaskScheduler.V2Interop.IRegisteredTask
RegisterTaskDefinition(System.String,
Microsoft.Win32.TaskScheduler.V2Interop.ITaskDefinition, Int32, System.Object,
System.Object, Microsoft.Win32.TaskScheduler.TaskLogonType, System.Object)
I post some code and hope you can help me. If you need more please ask for more.

To update/start the task i do this:
public static ExecAction WORKER_ACTION = new ExecAction(@"C:\aPath\Worker\Worker.exe", "", null);
...
this.CreateNewTask("Worker", "", new TimeTrigger(newDate), TaskHelper.WORKER_ACTION);
...

        public bool CreateNewTask(string taskname, string description, Trigger trigger, ExecAction action)
        {
            try
            {                
using (TaskService ts = new TaskService())
                {
                    TaskDefinition td = ts.NewTask();
                    td.RegistrationInfo.Description = description;
                    td.Triggers.Add(trigger);
                    td.Actions.Add(action);

                    ts.RootFolder.RegisterTaskDefinition(taskname, td, TaskCreation.CreateOrUpdate, "SYSTEM", null, TaskLogonType.ServiceAccount, null);
                }
...
I try it on a Windows Server 2008 R2.
Coordinator
Apr 24, 2014 at 3:57 PM
Just to be clear, this same code segment is called both times (first successfully and second failing), and the service that calls this code segment is the same and is running under the same user credentials with each call?
Coordinator
Apr 24, 2014 at 4:20 PM
By the way, the error you are getting typically means that there is a problem with the command-line path you are supplying to the ExecAction. Check to make sure it is not blank or corrupted on your second pass.
Marked as answer by dahall on 9/1/2015 at 10:07 AM
Apr 24, 2014 at 4:32 PM
Hi,

thanks for the answer. It was a corrupted ExecAction. Dont know why, but in the second call the static var WORKER_ACTION is empty...
I change the code and declare this var just before i call the method.

Thanks for your help.
Marked as answer by dahall on 9/1/2015 at 10:07 AM