This project has moved. For the latest updates, please go here.

Error on Scheduling as User in Vista

Nov 5, 2009 at 2:07 PM

Hi

I need to create a schedule task which runs weekly. I am using weekly trigger to schedule a task. This works fine with me for XP (logged in as Admin) & VIsta (logged in as Admin) but not in Vista (logged in as User). I need to schedule the task even when i am logged in as User. I am using this scheduler in two forms (Form 1 & Form 2). I am able to schedule tasks from Form1 but i am not able to schedule the task in Form2.

Here is the code which i am using in both the Forms (Form 1 & Form 2)

 

TaskService SchedulertaskService = new TaskService();

TaskFolder SchedulertFolder = SchedulertaskService.RootFolder;

Boolean bSchedulerSunday, bSchedulerMonday, bSchedulerTuesday, bSchedulerWednesday, bSchedulerThursday, bSchedulerFriday, bSchedulerSaturday;

String Day = DateTime.Today.Day.ToString();

String Month = DateTime.Today.Month.ToString();

String Year = DateTime.Today.Year.ToString();

String hh, mm, ss;

String Datestring;

DateTime dateValue;

Version ver = SchedulertaskService.HighestSupportedVersion;

bool newver = (ver == new Version(1, 2));

bSchedulerSunday = bSchedulerMonday = bSchedulerTuesday = bSchedulerWednesday = bSchedulerThursday = bSchedulerFriday = bSchedulerSaturday =false;


TaskDefinition SchedulertaskDefinition = SchedulertaskService.NewTask();

SchedulertaskDefinition.Data ="Data";

SchedulertaskDefinition.Principal.UserId = Environment.UserName;

SchedulertaskDefinition.Principal.LogonType = TaskLogonType.InteractiveToken;

SchedulertaskDefinition.Settings.DisallowStartIfOnBatteries =true;

SchedulertaskDefinition.Settings.Enabled =true;

SchedulertaskDefinition.Settings.IdleSettings.RestartOnIdle =false;

SchedulertaskDefinition.Settings.IdleSettings.StopOnIdleEnd =false;

SchedulertaskDefinition.Settings.IdleSettings.WaitTimeout =TimeSpan.FromMinutes(10);

SchedulertaskDefinition.Settings.Priority = System.Diagnostics.ProcessPriorityClass.High;

SchedulertaskDefinition.Settings.RunOnlyIfIdle =false;

SchedulertaskDefinition.Settings.RunOnlyIfNetworkAvailable =false;

if (newver)

{
SchedulertaskDefinition.Principal.RunLevel = TaskRunLevel.LUA;

SchedulertaskDefinition.RegistrationInfo.Version = new Version(0, 9);
SchedulertaskDefinition.Settings.AllowDemandStart = true;
SchedulertaskDefinition.Settings.AllowHardTerminate = true;
SchedulertaskDefinition.Settings.Compatibility = TaskCompatibility.V2;
SchedulertaskDefinition.Settings.StartWhenAvailable = true;
SchedulertaskDefinition.Settings.WakeToRun = false;
}
try
{
for  (Row = 0; Row <= uiSchedulerGrid.Rows.Count - 1; Row++)

{
foreach (Task task in SchedulertFolder.Tasks)

{
if (task.Name == "Scheduler " + uiSchedulerGrid.Rows[Row].Cells["Day"].Value.ToString() + " task")

{

 SchedulertaskService.RootFolder.DeleteTask(task.Name);
}

}

 hh = uiSchedulerGrid.Rows[Row].Cells["Time"].Value.ToString().Substring(0, 2);

 mm = uiSchedulerGrid.Rows[Row].Cells["Time"].Value.ToString().Substring(2, 2);
 ss = "00";
 switch (uiSchedulerGrid.Rows[Row].Cells["Day"].Value.ToString())
 {

 case "Sunday":

 if (uiSchedulerGrid.Rows[Row].Cells["Active"].Value.ToString() == "Yes")

{

Datestring = Month + "/" + Day + "/" + Year + " " + hh.ToString() + ":" + mm.ToString() + ":" + ss;

 dateValue = DateTime.Parse(Datestring);

TaskFolder SchedulertaskFolder = SchedulertaskService.RootFolder;
SchedulertaskDefinition = SchedulertaskService.NewTask();
SchedulertaskDefinition.RegistrationInfo.Description = "Scheduler"

WeeklyTrigger wTrigger = (WeeklyTrigger)SchedulertaskDefinition.Triggers.Add(new WeeklyTrigger());

wTrigger.DaysOfWeek = DaysOfTheWeek.Sunday;

wTrigger.StartBoundary = dateValue;
wTrigger.WeeksInterval = 1;
SchedulertaskDefinition.Actions.Add(new ExecAction("notepad.exe", " ", null));

SchedulertaskFolder.RegisterTaskDefinition("Scheduler " + uiSchedulerGrid.Rows[Row].Cells["Day"].Value.ToString() + " task", SchedulertaskDefinition, TaskCreation.CreateOrUpdate, null, null, TaskLogonType.InteractiveToken, null);
}

 break;

case "Monday":

 if (uiSchedulerGrid.Rows[Row].Cells["Active"].Value.ToString() == "Yes")

{

 Datestring = Month + "/" + Day + "/" + Year + " " + hh.ToString() + ":" + mm.ToString() + ":" + ss;

 dateValue = DateTime.Parse(Datestring);

 TaskFolder SchedulertaskFolder = SchedulertaskService.RootFolder;
 SchedulertaskDefinition = SchedulertaskService.NewTask();
 SchedulertaskDefinition.RegistrationInfo.Description = "Scheduler"

 WeeklyTrigger wTrigger = (WeeklyTrigger)SchedulertaskDefinition.Triggers.Add(new WeeklyTrigger());

  wTrigger.DaysOfWeek = DaysOfTheWeek.Monday;

  wTrigger.StartBoundary = dateValue;
 wTrigger.WeeksInterval = 1;
 SchedulertaskDefinition.Actions.Add(new ExecAction("notepad.exe", " ", null));

 SchedulertaskFolder.RegisterTaskDefinition("Scheduler " + uiSchedulerGrid.Rows[Row].Cells["Day"].Value.ToString() + " task", SchedulertaskDefinition, TaskCreation.CreateOrUpdate, null, null, TaskLogonType.InteractiveToken, null);

}

 break;

case "Tuesday":

 if (uiSchedulerGrid.Rows[Row].Cells["Active"].Value.ToString() == "Yes")

{

 Datestring = Month + "/" + Day + "/" + Year + " " + hh.ToString() + ":" + mm.ToString() + ":" + ss;

 dateValue = DateTime.Parse(Datestring);

 TaskFolder SchedulertaskFolder = SchedulertaskService.RootFolder;
 SchedulertaskDefinition = SchedulertaskService.NewTask();
 SchedulertaskDefinition.RegistrationInfo.Description = "Scheduler"

 WeeklyTrigger wTrigger = (WeeklyTrigger)SchedulertaskDefinition.Triggers.Add(new WeeklyTrigger());

  wTrigger.DaysOfWeek = DaysOfTheWeek.Tuesday;

  wTrigger.StartBoundary = dateValue;
 wTrigger.WeeksInterval = 1;
 SchedulertaskDefinition.Actions.Add(new ExecAction("notepad.exe", " ", null));

 SchedulertaskFolder.RegisterTaskDefinition("Scheduler " + uiSchedulerGrid.Rows[Row].Cells["Day"].Value.ToString() + " task", SchedulertaskDefinition, TaskCreation.CreateOrUpdate, null, null, TaskLogonType.InteractiveToken, null);

}

 break;
 

case "Wednesday":

 if (uiSchedulerGrid.Rows[Row].Cells["Active"].Value.ToString() == "Yes")

{

 Datestring = Month + "/" + Day + "/" + Year + " " + hh.ToString() + ":" + mm.ToString() + ":" + ss;

 dateValue = DateTime.Parse(Datestring);

 TaskFolder SchedulertaskFolder = SchedulertaskService.RootFolder;
 SchedulertaskDefinition = SchedulertaskService.NewTask();
 SchedulertaskDefinition.RegistrationInfo.Description = "Scheduler"

 WeeklyTrigger wTrigger = (WeeklyTrigger)SchedulertaskDefinition.Triggers.Add(new WeeklyTrigger());

  wTrigger.DaysOfWeek = DaysOfTheWeek.Wednesday;

  wTrigger.StartBoundary = dateValue;
 wTrigger.WeeksInterval = 1;
 SchedulertaskDefinition.Actions.Add(new ExecAction("notepad.exe", " ", null));

 SchedulertaskFolder.RegisterTaskDefinition("Scheduler " + uiSchedulerGrid.Rows[Row].Cells["Day"].Value.ToString() + " task", SchedulertaskDefinition, TaskCreation.CreateOrUpdate, null, null, TaskLogonType.InteractiveToken, null);

}

 break;


 case "Thursday":

 if (uiSchedulerGrid.Rows[Row].Cells["Active"].Value.ToString() == "Yes")

{

 Datestring = Month + "/" + Day + "/" + Year + " " + hh.ToString() + ":" + mm.ToString() + ":" + ss;

 dateValue = DateTime.Parse(Datestring);

 TaskFolder SchedulertaskFolder = SchedulertaskService.RootFolder;
 SchedulertaskDefinition = SchedulertaskService.NewTask();
 SchedulertaskDefinition.RegistrationInfo.Description = "Scheduler"

 WeeklyTrigger wTrigger = (WeeklyTrigger)SchedulertaskDefinition.Triggers.Add(new WeeklyTrigger());

  wTrigger.DaysOfWeek = DaysOfTheWeek.Thursday;

 wTrigger.StartBoundary = dateValue;
 wTrigger.WeeksInterval = 1;
 SchedulertaskDefinition.Actions.Add(new ExecAction("notepad.exe", " ", null));

 SchedulertaskFolder.RegisterTaskDefinition("Scheduler " + uiSchedulerGrid.Rows[Row].Cells["Day"].Value.ToString() + " task", SchedulertaskDefinition, TaskCreation.CreateOrUpdate, null, null, TaskLogonType.InteractiveToken, null);

}

break;

case "Friday":


 if (uiSchedulerGrid.Rows[Row].Cells["Active"].Value.ToString() == "Yes")

{

Datestring = Month + "/" + Day + "/" + Year + " " + hh.ToString() + ":" + mm.ToString() + ":" + ss;

dateValue = DateTime.Parse(Datestring);

TaskFolder SchedulertaskFolder = SchedulertaskService.RootFolder;
SchedulertaskDefinition = SchedulertaskService.NewTask();
SchedulertaskDefinition.RegistrationInfo.Description = "Scheduler"

WeeklyTrigger wTrigger = (WeeklyTrigger)SchedulertaskDefinition.Triggers.Add(new WeeklyTrigger());

 wTrigger.DaysOfWeek = DaysOfTheWeek.Friday;

 wTrigger.StartBoundary = dateValue;
 wTrigger.WeeksInterval = 1;
 SchedulertaskDefinition.Actions.Add(new ExecAction("notepad.exe", " ", null));

 SchedulertaskFolder.RegisterTaskDefinition("Scheduler " + uiSchedulerGrid.Rows[Row].Cells["Day"].Value.ToString() + " task", SchedulertaskDefinition, TaskCreation.CreateOrUpdate, null, null, TaskLogonType.InteractiveToken, null);

}

break;

case "Saturday":


 if (uiSchedulerGrid.Rows[Row].Cells["Active"].Value.ToString() == "Yes")

{

Datestring = Month + "/" + Day + "/" + Year + " " + hh.ToString() + ":" + mm.ToString() + ":" + ss;

dateValue = DateTime.Parse(Datestring);

TaskFolder SchedulertaskFolder = SchedulertaskService.RootFolder;
SchedulertaskDefinition = SchedulertaskService.NewTask();
SchedulertaskDefinition.RegistrationInfo.Description = "Scheduler"

WeeklyTrigger wTrigger = (WeeklyTrigger)SchedulertaskDefinition.Triggers.Add(new WeeklyTrigger());

 wTrigger.DaysOfWeek = DaysOfTheWeek.Saturday;

 wTrigger.StartBoundary = dateValue;
 wTrigger.WeeksInterval = 1;
 SchedulertaskDefinition.Actions.Add(new ExecAction("notepad.exe", " ", null));

 SchedulertaskFolder.RegisterTaskDefinition("Scheduler " + uiSchedulerGrid.Rows[Row].Cells["Day"].Value.ToString() + " task", SchedulertaskDefinition, TaskCreation.CreateOrUpdate, null, null, TaskLogonType.InteractiveToken, null);

}

 break;

}

}

}

catch (Exception ex)

{

if (ex.Message.Equals("Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"))

{

FrmErrorForm Errfrm = new FrmErrorForm("You must be logged on as administrator to schedule tasks", ex.Message + Environment.NewLine + ex.Source);

Errfrm.ShowDialog();

}

else

{

FrmErrorForm Errfrm = new FrmErrorForm("Error in scheduler", ex.Message + Environment.NewLine + ex.Source);

Errfrm.ShowDialog();

}

}

 

 

I dont know where i am going wrong.

 Any Suggestions?

 

Coordinator
Nov 9, 2009 at 5:17 PM

I'm not really sure how to the follow the code. There is a lot of redundancy. I pasted it into my test harness, cleaned it up a lot, and ran it. You had a few properties that are only available to Admin or System accounts. I removed those. This works for each of the account types. You were recreating the TaskDefinition without bringing down any of the work done at the start of the method using NewTask(). You also were repeating the exact same code within you switch statement. Any way, hope this works for you.

TaskService SchedulertaskService = new TaskService();
TaskFolder SchedulertFolder = SchedulertaskService.RootFolder;
Boolean bSchedulerSunday, bSchedulerMonday, bSchedulerTuesday, bSchedulerWednesday, bSchedulerThursday, bSchedulerFriday, bSchedulerSaturday;
bSchedulerSunday = bSchedulerMonday = bSchedulerTuesday = bSchedulerWednesday = bSchedulerThursday = bSchedulerFriday = bSchedulerSaturday =false;

TaskDefinition SchedulertaskDefinition = SchedulertaskService.NewTask();
SchedulertaskDefinition.Data = "Data";
SchedulertaskDefinition.Principal.LogonType = TaskLogonType.InteractiveToken;
SchedulertaskDefinition.Settings.DisallowStartIfOnBatteries =true;
SchedulertaskDefinition.Settings.Enabled =true;
SchedulertaskDefinition.Settings.IdleSettings.RestartOnIdle =false;
SchedulertaskDefinition.Settings.IdleSettings.StopOnIdleEnd =false;
SchedulertaskDefinition.Settings.IdleSettings.WaitTimeout =TimeSpan.FromMinutes(10);
SchedulertaskDefinition.Settings.RunOnlyIfIdle =false;
SchedulertaskDefinition.Settings.RunOnlyIfNetworkAvailable =false;
SchedulertaskDefinition.RegistrationInfo.Description = "Scheduler";

WeeklyTrigger wTrigger = (WeeklyTrigger)SchedulertaskDefinition.Triggers.Add(new WeeklyTrigger());
wTrigger.WeeksInterval = 1;

SchedulertaskDefinition.Actions.Add(new ExecAction("notepad.exe", " ", null));

if (SchedulertaskService.HighestSupportedVersion == new Version(1, 2))
{
  SchedulertaskDefinition.RegistrationInfo.Version = new Version(0, 9);
  SchedulertaskDefinition.Settings.AllowDemandStart = true;
  SchedulertaskDefinition.Settings.AllowHardTerminate = true;
  SchedulertaskDefinition.Settings.StartWhenAvailable = true;
  SchedulertaskDefinition.Settings.WakeToRun = false;
}
try
{
  for (Row = 0; Row <= uiSchedulerGrid.Rows.Count - 1; Row++)
  {
    foreach (Task task in SchedulertFolder.Tasks)
    {
      if (task.Name == "Scheduler " + uiSchedulerGrid.Rows[Row].Cells["Day"].Value.ToString() + " task")
      {
        SchedulertaskService.RootFolder.DeleteTask(task.Name);
      }
    }

    if (uiSchedulerGrid.Rows[Row].Cells["Active"].Value.ToString() == "Yes")
    {
      wTrigger.DaysOfWeek = (DaysOfTheWeek)Enum.Parse(typeof(DaysOfTheWeek), uiSchedulerGrid.Rows[Row].Cells["Day"].Value.ToString(), true);
      int hh = int.Parse(uiSchedulerGrid.Rows[Row].Cells["Time"].Value.ToString().Substring(0, 2));
      int mm = int.Parse(uiSchedulerGrid.Rows[Row].Cells["Time"].Value.ToString().Substring(2, 2));
      wTrigger.StartBoundary = DateTime.Today + new TimeSpan(hh, mm, 0);
      SchedulertFolder.RegisterTaskDefinition("Scheduler " + uiSchedulerGrid.Rows[Row].Cells["Day"].Value.ToString() + " task", SchedulertaskDefinition);
    }
  }
}
catch (Exception ex)
{
  if (ex.Message.Equals("Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"))
  {
    FrmErrorForm Errfrm = new FrmErrorForm("You must be logged on as administrator to schedule tasks", ex.Message + Environment.NewLine + ex.Source);
    Errfrm.ShowDialog();
  }
  else
  {
    FrmErrorForm Errfrm = new FrmErrorForm("Error in scheduler", ex.Message + Environment.NewLine + ex.Source);
    Errfrm.ShowDialog();
  }
}