基於什麼McNets在his comment說:
爲什麼不使用std。 Windows關機命令?添加一個計劃任務,立即關閉系統
我決定利用窗口的任務計劃程序。我決定使用Task Scheduler,但也有其他人做類似的事情。
儘管這種方法比打開命令提示符更復雜一些,但它完成了我所需要的一切,包括規避了我在問題中提到的關閉橫幅。請注意,我仍然可以讓用戶選擇像我以前那樣使用橫幅來運行它,但我尚未實現該功能。這是相當微不足道的(特別是下面的基本代碼)。只需將計時器添加到關閉參數而不是/ t 1,並將任務設置爲立即運行。
這可能不是唯一的解決方案,但它是我能找到的侵入性最小的方法。謝謝各位的幫助。我很感激。
Full Source on GitHub。
全局:
public const string DEFAULT_TASK_NAME = "ScheduledShutdownTimer";
private const string SHUTDOWN_COMMAND_ARGS = "/s /c \"Scheduled Computer shutdown via " +
"the Windows Shutdown Timer App\" /t 1";
創建任務:
using (TaskService ts = new TaskService())
{
// If the task doesn't exist, create it.
if (TimerExists(DEFAULT_TASK_NAME))
throw new TimerExists("The timer already exists in the task scheduler. You " +
"must modify it instead of attempting to create it!");
else
{
try
{
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Date = _currentTime; // DateTime.Now
td.RegistrationInfo.Source = "Windows Shutdown Timer";
td.RegistrationInfo.Description = "Shutdown Timer initiated Windows " +
"Shutdown Timer";
td.Settings.Enabled = true;
td.Triggers.Add(new TimeTrigger(_shutdownTime));
td.Actions.Add(new ExecAction("shutdown", SHUTDOWN_COMMAND_ARGS, null));
TaskService.Instance.RootFolder
.RegisterTaskDefinition(DEFAULT_TASK_NAME,td);
Properties.Settings.Default.ShutdownTimer = _shutdownTime;
Properties.Settings.Default.Save();
StartLocalTimer();
}
catch(Exception)
{
DialogResult alert = MessageBox.Show("The timer couldn't be set. ",
"Error - Couldn't Set Timer!", MessageBoxButtons.RetryCancel,
MessageBoxIcon.Error);
if (alert == DialogResult.Retry)
CreateShutdownTimer(numSeconds);
}
}
}
修改一個任務:
using (TaskService ts = new TaskService())
{
// If the task exists, update the trigger.
if (TimerExists(DEFAULT_TASK_NAME))
{
Task task = ts.GetTask(DEFAULT_TASK_NAME);
if (task.Definition.Triggers.Count == 1)
task.Definition.Triggers.RemoveAt(0);
else if (task.Definition.Triggers.Count > 1)
{
for (int index = 0; index < task.Definition.Triggers.Count - 1; index++)
{
task.Definition.Triggers.RemoveAt(index);
}
}
// Add the new trigger after making sure it is the only one.
task.Definition.Triggers.Add(new TimeTrigger(_shutdownTime));
if (task.Definition.Actions.Count == 1)
task.Definition.Actions.RemoveAt(0);
else if (task.Definition.Actions.Count > 1)
{
for (int index = 0; index < task.Definition.Actions.Count - 1; index++)
{
task.Definition.Actions.RemoveAt(index);
}
}
// Add the new action after making sure it is the only one.
task.Definition.Actions.Add(new ExecAction("shutdown", SHUTDOWN_COMMAND_ARGS,
null));
// Reset the status in case it was set as anything but "Ready"
task.Definition.Settings.Enabled = true;
task.RegisterChanges();
Properties.Settings.Default.ShutdownTimer = _shutdownTime;
Properties.Settings.Default.Save();
// Starts the timer display and enables/disables buttons.
StartLocalTimer();
}
else
throw new NoTimerExists("The timer doesn't exist in the task scheduler. You " +
"must create it instead of attempting to modify it!");
}
停止任務:
using (TaskService ts = new TaskService())
{
// If the task exists, remove the trigger.
// Note: the included Stop() method doesn't work.
if (TimerExists(DEFAULT_TASK_NAME))
{
Task task = ts.GetTask(DEFAULT_TASK_NAME);
task.Definition.Triggers.RemoveAt(0);
task.RegisterChanges();
StopLocalTimer(); // Resets display timers in program
}
else
throw new NoTimerExists("The timer doesn't exist in the task scheduler. " +
"You must create it instead of attempting to modify it!");
}
爲什麼不使用std。 windows'shutdown'命令?添加一個立即關閉系統的計劃任務。 – McNets
目前我使用shutdown -s -t [time]。如果您建議我使用超時功能,那麼問題是如果用戶關閉程序,我無法準確檢查是否有關閉命令正在運行。但如果你建議我添加一個計劃任務(在任務計劃程序中),那麼這是可能的,但我想要真的避免出於其他原因。但如果這是唯一的解決方案,那麼就這樣吧。我會快速拍攝! –
只是八卦。你爲什麼試圖關閉電腦而不提醒用戶? – McNets