无论用户是否登录,我都需要计划任务来执行exe。
我尝试使用将.Settings.RunOnlyIfLoggedOn
布尔值设置为false
的任务来创建任务,但是当我尝试创建任务时遇到了以下错误:
Task Scheduler 2.0(1.2)不支持设置此属性。您 必须使用InteractiveToken才能使任务在 当前的用户会话。
我尝试添加TaskLogonType.InteractiveTokenOrPassword
选项,但仍然遇到相同的错误。
如何使用C#激活此功能?
下面是我用来创建任务的代码:
#region [CREATE]
createTask:
if (fileName_txtbox.Text.Any(Path.GetInvalidFileNameChars().Contains)) {
MessageBox.Show("The report file name contains invalid characters!", "Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
//try {
// We create a new empty scheduled task
TaskDefinition _cTsk = Global.TaskSrvc.NewTask();
_cTsk.Principal.LogonType = TaskLogonType.InteractiveTokenOrPassword;
#region [CONFIGURATION_FILE]
string[] _dbStructure = File.ReadAllLines(String.Format("{0}\\scheduled_configuration.xs8db",
Form1.Global.MainConfigDirectory + @"\Databases\" + Form1.Global.SelectedDBName));
// Database last report
_dbStructure[2] = Form1.Global.Encrypt("Scheduled report not yet run");
// Set output directory
_dbStructure[3] = Form1.Global.Encrypt(directory_txtbox.Text);
// Set file name
_dbStructure[4] = Form1.Global.Encrypt(fileName_txtbox.Text);
// Set SQL command
_dbStructure[5] = Form1.Global.Encrypt(sql_txtbox.Text);
File.WriteAllLines(String.Format("{0}\\scheduled_configuration.xs8db",
Form1.Global.MainConfigDirectory + @"\Databases\" + Form1.Global.SelectedDBName),
_dbStructure);
#endregion
#region [SCHEDULED_TASK]
#region [GENERAL]
// Registration Info
_cTsk.RegistrationInfo.Author = "Mutu Adi-Marian";
_cTsk.RegistrationInfo.Date = DateTime.Today;
_cTsk.RegistrationInfo.Description = "This task will automatically launch a query report against the database.";
// Principal
_cTsk.Principal.RunLevel = TaskRunLevel.Highest;
#endregion
#region [SETTINGS]
_cTsk.Settings.Enabled = true;
_cTsk.Settings.RunOnlyIfLoggedOn = false; // <-- THE PROBLEM
_cTsk.Settings.AllowDemandStart = true;
_cTsk.Settings.AllowHardTerminate = false;
_cTsk.Settings.DisallowStartIfOnBatteries = false;
_cTsk.Settings.MultipleInstances = TaskInstancesPolicy.Queue;
_cTsk.Settings.Priority = System.Diagnostics.ProcessPriorityClass.High;
_cTsk.Settings.RestartCount = 5;
_cTsk.Settings.RestartInterval = TimeSpan.FromMinutes(1);
_cTsk.Settings.RunOnlyIfIdle = false;
_cTsk.Settings.StartWhenAvailable = true;
_cTsk.Settings.StopIfGoingOnBatteries = false;
_cTsk.Settings.WakeToRun = true;
#endregion
#region [TRIGGERS]
// Get the Hour from the day of time control
double _ch = Convert.ToDouble(timeOfDay_masktxtbox.Text.Substring(0, 2));
// Get the Minutes from the day of time control
double _cm = Convert.ToDouble(timeOfDay_masktxtbox.Text.Substring(3, 2));
// Get if the task will be triggered by days of week or by days of month
if (weekMo_cbx.Enabled) {
DaysOfTheWeek _dow = DaysOfTheWeek.AllDays;
if (!weekMo_cbx.Checked)
_dow -= DaysOfTheWeek.Monday;
if (!weekTu_cbx.Checked)
_dow -= DaysOfTheWeek.Tuesday;
if (!weekWe_cbx.Checked)
_dow -= DaysOfTheWeek.Wednesday;
if (!weekTh_cbx.Checked)
_dow -= DaysOfTheWeek.Thursday;
if (!weekFr_cbx.Checked)
_dow -= DaysOfTheWeek.Friday;
if (!weekSa_cbx.Checked)
_dow -= DaysOfTheWeek.Saturday;
if (!weekSu_cbx.Checked)
_dow -= DaysOfTheWeek.Sunday;
WeeklyTrigger _tskWt = new WeeklyTrigger(_dow) {
StartBoundary = DateTime.Today + (TimeSpan.FromHours(_ch) + TimeSpan.FromMinutes(_cm))
};
_cTsk.Triggers.Add(_tskWt);
} else {
int[] _dom = new int[byDaysOfMonths_listcbx.CheckedIndices.Cast<int>().ToArray().Count()];
for (UInt16 i = 0; i < _dom.Length; i++) {
_dom[i] = Convert.ToInt32(byDaysOfMonths_listcbx.CheckedItems.Cast<string>().ToArray()[i]);
}
MonthlyTrigger _tskMt = new MonthlyTrigger {
DaysOfMonth = _dom,
StartBoundary = DateTime.Today + (TimeSpan.FromHours(_ch) + TimeSpan.FromMinutes(_cm))
};
_cTsk.Triggers.Add(_tskMt);
}
#endregion
#region [ACTIONS]
// Add the updated action
_cTsk.Actions.Add(Global.ReportRunnerPath, String.Format("\"{0}\" \"{1}\" \"{2}\"", Form1.Global.SelectedDBName, fileName_txtbox.Text, directory_txtbox.Text));
#endregion
#endregion
// Save the created task in the 'Database Query Scheduler' folder
Global.TaskSrvc.RootFolder.RegisterTaskDefinition(String.Format("{0}\\{1}", Global.TaskMainFolder, Form1.Global.SelectedDBName), _cTsk);
// Enable the 'Test Report' button
Global.CanTestReport = true;
MessageBox.Show("Scheduled report successfully created!", "Info");
changesNotSaved_lbl.Visible = false;
return;
/*} catch {
MessageBox.Show("Unable to create the 'Scheduled Report'", "Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}*/