简体   繁体   English

C# 任务计划程序触发器不起作用

[英]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).它在 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 .我的直接猜测是t.Triggers当前等于null

You may need to instantiate a Triggers Collection in t.Triggers before trying to add anything to it.在尝试向其中添加任何内容之前,您可能需要在t.Triggers中实例化一个触发器集合。

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.对于想要使用 ASP.NET 代码中的任务计划程序的客户,通常会出现此问题。 Ordinarily, such code runs in the ASPNET account which has rather low privilege and can't use the Task Scheduler.通常,此类代码在 ASPNET 帐户中运行,该帐户具有较低的权限,无法使用任务计划程序。 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.这称为模拟,您可以在 web.config 文件中进行设置。

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.在 Windows XP SP2 中,似乎有一些概括。 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. Read = 查看它,Read/Execute = 运行任务,Write = 修改任务。

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.我使用这个特定的库: http://taskscheduler.codeplex.com/ ...它正在积极开发,最新版本于上个月发布,五月。 It automatically uses the proper version of the Task Scheduler depending on the OS.它会根据操作系统自动使用正确版本的任务计划程序。 Its just a personal preference...这只是个人喜好...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM