5

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

Quinxy von Besiex
  • 976
  • 11
  • 21
  • 1
    The task scheduler interface is COM based, you'll need IScheduledWorkItem::SetFlags() for example. There is no "C# TaskManager object". Maybe you ought to post a link to code you found somewhere? – Hans Passant Nov 17 '11 at 16:54
  • Thanks! I ended up discovering it on my own, but you were right of course about me having used a third-party wrapper, I didn't realize that! I posted the solution. – Quinxy von Besiex Nov 17 '11 at 18:41
  • @QuinxyvonBesiex You should add your answer below and then after the amount of time require, select it as the answer - keeps things clean – Prescott Jan 17 '12 at 19:12

1 Answers1

4

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!

bkane521
  • 386
  • 2
  • 10
Quinxy von Besiex
  • 976
  • 11
  • 21
  • There is more to it than just setting the Logon type in some cases. If this answer alone doesn't get your code working, see this question:http://stackoverflow.com/questions/43599271/how-to-set-run-only-if-logged-in-and-run-as-with-taskscheduler-in-c/43767201#43767201 – Roger Hill May 03 '17 at 18:11