简体   繁体   中英

How to set “run only if logged in” and “run as” with TaskScheduler in C#?

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.

Q

Okay, I figured out the answer!

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;

Thanks for trying to help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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