This project has moved and is read-only. For the latest updates, please go here.

Run existing task

Jun 10, 2011 at 3:14 PM

I hate to ask something so simple but I just can't figure it out and I have posted this on multiple forums.  I am trying to set up a button that, when clicked, runs an existing task.  I have created a variable that gets a selected task but when I try and set up the task, it always returns null.


using (TaskService ts = new TaskService())
                            string task = rdr["TaskName"].ToString();

                            TaskDefinition td = ts.NewTask();
                            td.RegistrationInfo.Description = "Get Tasks";
                            td.Principal.LogonType = TaskLogonType.InteractiveToken;

                            t = ts.GetTask(task);
                            td = t.Definition;
                            if (t != null)
                                td.Triggers.Add(new TimeTrigger { StartBoundary = DateTime.Now });
task always shows the correct value but it never sets t to the correct value.  It is always just null.

Jun 10, 2011 at 4:13 PM

I believe this code will accomplish the task:

using (TaskService ts = new TaskService())
   string task = rdr["TaskName"].ToString();
   // This will find it even if its down in a folder. Alternately you could call:
   // Task t = ts.GetTask(task);
   Task t = ts.FindTask(task);
   if (t != null)
Jun 10, 2011 at 5:01 PM

When stepping through, t is still null.  I tried GetTask and FindTask.  With t.Run, the application no longer crashes on me though.

Jun 10, 2011 at 8:38 PM

When you go into the Task Scheduler Management Console, make sure the task name you're using matches exactly and doesn't have leading or trailing spaces. The FindTask method specifically will always find the task if the name matches. If t == null, then the name you have specified is not already registered (created) or you don't have permissions to see it.

Jun 13, 2011 at 2:31 PM

The only thing that I have noticed is when it pulls the scheduled task, the name is ScheduledTask.job.  Do you think the .job would throw it off?

Jun 13, 2011 at 3:22 PM
Edited Jun 13, 2011 at 3:23 PM

I see an error on my FindTask statement.  The error says "v2InvalidDate: Cannot dereference expression.  The pointer is not valid."  Also, I have removed the ".job" from the string.

Jun 13, 2011 at 4:43 PM

v2InvalidDate is only accessible when pulling the LastRunTime property from a task. I'm not sure how FindTask would even see it.

And yes, having ".job" as part of the name string will cause problem with version 1 tasks.

Jun 13, 2011 at 7:37 PM
Edited Jun 13, 2011 at 8:01 PM

I had put in next run time in order to see if it was doing anything.  It will not because t is always null.  I stopped using my variable and manually put in the exact name of the task and it still does not work.  This is Server 2003 so I believe it is Version 1 correct?  Could it be that it can not find the path to the Scheduled Task?

I can not figure out what I am doing wrong.

Jun 13, 2011 at 9:31 PM

Maybe it will help if I post the whole code.


 using (SqlConnection myConnection2 = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString))
                SqlCommand cmd2 = new SqlCommand("SELECT ID, TaskName, Permission, from ScheduledTasks s, Roles r WHERE s.ID=@ID ", myConnection2);
                cmd2.Parameters.AddWithValue("@ID", txtTaskID.Text);

                SqlDataReader rdr;
                rdr = cmd2.ExecuteReader();

                if (rdr.HasRows)
                    while (rdr.Read())
                        using (TaskService ts = new TaskService())
                            string task = rdr["TaskName"].ToString();
                            string newName = task.Remove(task.Length - 4, 4);

                            t = ts.FindTask(newName);
                            if (t != null)

Jun 14, 2011 at 5:06 AM

Try the following and tell me if it works:

t = ts.RootFolder.Tasks[newName];

Jun 14, 2011 at 2:07 PM
Edited Jun 14, 2011 at 5:02 PM

It cannot find the path specified.

Jun 14, 2011 at 5:09 PM

I just noticed that the target server is showing up as my PC.  I am going to have to figure out a way for it to dynamically get the Server IP.

Jun 14, 2011 at 10:42 PM

I pulled in the server variable and it works great.  Thanks for all of your help.

Aug 25, 2011 at 7:48 AM

I tried to run the task after i do a FindTask, but it seems not to be working in Windows7 pc. I checked in a XP machine, and it ran the task successfully.

Can anyone pls help?

Aug 25, 2011 at 8:45 AM

Its resolved.