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

发布于 2024-11-14 11:15:57 字数 1073 浏览 1 评论 0原文

我不知道是否有人可以提供帮助,但当我运行应用程序时,我收到错误(对象引用未设置为对象的实例)。这是代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼角的笑意。 2024-11-21 11:15:57

我的直接猜测是 t.Triggers 当前等于 null

在尝试向其中添加任何内容之前,您可能需要在 t.Triggers 中实例化一个触发器集合。

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.

总攻大人 2024-11-21 11:15:57

您似乎按照作者演示的方式使用它。您收到的错误是什么?请发布异常消息。

来自作者常见问题解答:

为什么我会遇到访问异常?

这个问题通常出现在
想要使用任务的客户
来自 ASP.NET 代码的调度程序。
通常,这样的代码运行在
ASPNET 帐户具有相当低的
权限并且无法使用任务
调度程序。解决这个问题的方法是
将您的代码设置为在另一个、更多的环境中运行
特权帐户。这就是所谓的
模拟,你可以设置它
在您的 web.config 文件中。

任务计划程序不需要
客户端以管理方式运行
特权,但如果没有,就会有
对可以做的事情进行限制。我
还没有发现这些
有据可查。然而,直到
最近似乎
非管理员可以看到并
操纵他们创建的任务,但是
没有其他人。在 Windows XP SP2 中,有
似乎有些概括。在
资源管理器,有一个新的安全性
任务属性对话框上的选项卡。
还有做一点
解释该文件的文档
任务的权限将受到控制
其他用户可以用它们做什么。
读取=查看,读取/执行=运行
任务,写入=修改任务。

我还想知道,您没有提供该任务应该做什么的任何细节。正如某些事情应该发生一样,当任务运行时,需要执行一些程序。我假设您在发布之前删除了这些行。如果没有,那么这个库可能不允许创建没有关于在计划时间执行哪个应用程序的信息的任务。

我使用这个特定的库: 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:

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 ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文