如何设置“仅在登录后运行"和“运行"在 C# 中使用 TaskScheduler? [英] How to set "run only if logged in" and "run as" with TaskScheduler in C#?

查看:36
本文介绍了如何设置“仅在登录后运行"和“运行"在 C# 中使用 TaskScheduler?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用 C# TaskManager 对象创建任务的代码.在 Windows 7 上它工作正常,但在 Windows XP(可能还有其他 Windows)上它根本不起作用,因为任务的默认用户是 system,因此没有显示 GUI 的会话.如果我在控制面板小部件中手动修改创建的任务以将作业设置为仅在用户登录时运行且仅针对特定用户运行,则一切正常.但是,尽管进行了数小时的搜索,我还是看不到在 C# 对象中设置这些选项的选项.有人知道现有对象的解决方案吗?我不想重写所有内容以手动运行调度程序 EXE 并通过命令行传递内容.

I've got code that uses the C# TaskManager object to create a task. On Windows 7 it works fine but on Windows XP (and presumably other Windows) it doesn't work at all because the default user for the task is system and thus there's no session for the GUI to be displayed. If I modify the created task manually in the control panel widget to set the job to run only when user is logged in and only for the particular user, then everything works perfectly. But despite hours of searching I see no options for setting these options in the C# objects. Anyone know a solution with the existing objects? I'd hate to rewrite everything to manually run the scheduler EXE and pass in stuff by command-line.

推荐答案

好的,我找到答案了!

我没有意识到,但我一直在使用第三方 Task Scheduler Managed Wrapper(它自从我写了那部分代码以来已经有一段时间了)这就解释了为什么很难找到帮助!我刚才偶然发现了那个页面,在他们的例子中正是我所需要的!详细的上下文解决方案可以在这里找到,但关键部分是:

I didn't realize it but I had been using a third-party Task Scheduler Managed Wrapper (it'd been a while since I wrote that part of my code) and that explains why help was hard to find! I stumbled across that page a moment ago and right there in their examples was just what I needed! The detailed solution in context can be found here, but the key part is:

// Create a new task definition and assign properties
TaskDefinition td = ts.NewTask();
td.Principal.UserId = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
td.Principal.LogonType = TaskLogonType.InteractiveToken;

感谢您的帮助!

这篇关于如何设置“仅在登录后运行"和“运行"在 C# 中使用 TaskScheduler?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆