C# Task Scheduler Trigger not working
I don't know if anyone can help with this but I am getting an error (object reference not set to an instance of an object) when I run my application. Here is the code:
using (SqlConnection myConnection2 = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString))
{
myConnection2.Open();
SqlCommand cmd2 = new SqlCommand("SELECT ID, TaskName, Permission from ScheduledTasks s, Roles r WHERE s.ID= " + txtTaskID.Text, myConnection2);
SqlDataReader rdr;
rdr = cmd2.ExecuteReader();
if (rdr.HasRows)
{
while (rdr.Read())
{
string task = rdr["TaskName"].ToString();
Trigger tg = new RunOnceTrigger(DateTime.Now);
ScheduledTasks st = new ScheduledTasks();
Task t = st.OpenTask(task);
t.Triggers.Add(tg);
t.Save();
}
}
}
It errors in the line t.Triggers.Add(tg). I have stepped through the code and task is storing the right task name. 开发者_运维技巧 It just won't start the the task.
My immediate guess would be that t.Triggers
is currently equal to null
.
You may need to instantiate a Triggers Collection in t.Triggers
before trying to add anything to it.
You seem to be using it the way the author demonstrated. Whats the error that you are getting? Please post the exception message.
From the authors FAQ:
Why am I getting access exceptions?
This problem usually comes up for clients who want to use the Task Scheduler from ASP.NET code. Ordinarily, such code runs in the ASPNET account which has rather low privilege and can't use the Task Scheduler. The solution to this is to set your code to run in another, more privileged account. This is called impersonation, and you can set it up in your web.config file.
The Task Scheduler doesn't require the client to run with administrative privilege, but if it's not, there will be restrictions on what can be done. I haven't found these to be well-documented. However, until recently it seemed that non-administrators could see and manipulate the tasks they created, but no others. In Windows XP SP2, there seems to be some generalization. In the Explorer, there is a new Security tab on the task Properties dialog box. There is also do a little documentation explaining that the file permissions on the task will govern what other users can do with them. Read = look at it, Read/Execute = run the task, Write = modify the task.
I was also wondering, that you havent provided any details for WHAT the task is supposed to do. As in something ought to happen, some program needs to be executed, when the task is run. I am assuming that you removed those lines before posting. If not, then maybe this library doesnt allow creating tasks without information on which application to execute at the scheduled time.
I use this particular library: http://taskscheduler.codeplex.com/ ... its actively developed, the latest version was released last month, in may. It automatically uses the proper version of the Task Scheduler depending on the OS. Its just a personal preference ...
精彩评论