C# 任务计划程序触发器不起作用
我不知道是否有人可以提供帮助,但当我运行应用程序时,我收到错误(对象引用未设置为对象的实例)。这是代码:
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();
}
}
}
t.Triggers.Add(tg) 行出错。我已单步执行代码,任务正在存储正确的任务名称。它只是不会启动任务。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的直接猜测是
t.Triggers
当前等于null
。在尝试向其中添加任何内容之前,您可能需要在 t.Triggers 中实例化一个触发器集合。
My immediate guess would be that
t.Triggers
is currently equal tonull
.You may need to instantiate a Triggers Collection in
t.Triggers
before trying to add anything to it.您似乎按照作者演示的方式使用它。您收到的错误是什么?请发布异常消息。
来自作者常见问题解答:
我还想知道,您没有提供该任务应该做什么的任何细节。正如某些事情应该发生一样,当任务运行时,需要执行一些程序。我假设您在发布之前删除了这些行。如果没有,那么这个库可能不允许创建没有关于在计划时间执行哪个应用程序的信息的任务。
我使用这个特定的库: http://taskscheduler.codeplex.com/ ...它正在积极开发,最新版本于上个月(五月)发布。它会根据操作系统自动使用正确版本的任务计划程序。这只是个人喜好...
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:
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 ...