如何作为Windows任务计划程序计划任务运行时隐藏程序的窗口? [英] How to hide program's window when running as a Windows Task Scheduler scheduled task?
本文介绍了如何作为Windows任务计划程序计划任务运行时隐藏程序的窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用C#代码创建了一个任务
I have created a task using C# code
public void CreateSchedule()
{
string path = Directory.GetCurrentDirectory();
path = path + "\\shedularEXE";
using (TaskService ts = new TaskService())
{
// Create a new task definition and assign properties
TaskDefinition td = ts.NewTask();
td.Settings.MultipleInstances = TaskInstancesPolicy.IgnoreNew;
td.RegistrationInfo.Description = "working with network failOver";
td.Principal.LogonType = TaskLogonType.InteractiveToken;
// Add a trigger that will fire the task at this time every other day
DailyTrigger dt = (DailyTrigger)td.Triggers.Add(new DailyTrigger(1));
// dt.Repetition.Duration = TimeSpan.FromMinutes(10);
dt.Repetition.Interval = TimeSpan.FromHours(4d);
// Add a trigger that will fire every week on FridayDaysOfTheWeek.Friday
//td.Triggers.Add(new WeeklyTrigger(DaysOfTheWeek.AllDays, 1));// + TimeSpan.FromHours(2), DaysOfWeek = DaysOfTheWeek.Friday });
td.Actions.Add(new ExecAction(path ,"", null));
// td.Actions.Add(new ExecAction(@"C:\Users\Administrator\Documents\Visual Studio 2008\Projects\RunTask\shedularEXE\bin\Debug\shedularEXE", "", null));
// Register the task in the root folder
const string taskName = "Sync All Database";
ts.RootFolder.RegisterTaskDefinition(taskName, td);
// Retrieve the task, change the trigger and re-register it
Task t = ts.GetTask(taskName);
td = t.Definition;
td.Triggers[0].StartBoundary = DateTime.Now.AddMinutes(10);//.Today;//+ TimeSpan.FromDays(7);
ts.RootFolder.RegisterTaskDefinition(taskName, td);
// ts.RootFolder.DeleteTask(taskName);
}
}
工作正常,但我担心的是每当任务触发器触发时,它都会弹出一个cmd窗口的taskeng.exe。
我想隐藏这个命令窗口。
its working fine but my concern is that whenever task trigger is firing it will pop up one cmd window taskeng.exe.
I want to hide this command window.
推荐答案
这是我用来测试Hidden属性的代码。我无法确定如何使用任务计划程序与上面的代码中的对象名称。下面的代码是在Windows 8 PC上使用Visual Studio 2012开发的。
我在测试中发现当时隐藏属性不受尊重任务注册时,_TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN
用于_TASK_LOGON_TYPE.TASK_LOGON_PASSWORD
。
This is the code that I used to test the Hidden attribute. I was unable to determine how to use Task Scheduler with the object names that are in your code above. The code below was developed using Visual Studio 2012 on a Windows 8 PC.
I found during my testing that the Hidden Attribute is not honored when_TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN
is used in place_TASK_LOGON_TYPE.TASK_LOGON_PASSWORD
when the task is registered.
public void CreateSchedule()
{
ITaskService ts =new TaskScheduler.TaskScheduler ();
ts.Connect();
// Create a new task definition and assign properties
ITaskDefinition td = ts.NewTask(0);
td.Settings.MultipleInstances = _TASK_INSTANCES_POLICY.TASK_INSTANCES_IGNORE_NEW ;
td.RegistrationInfo.Description = "working with network failOver";
td.Principal.LogonType = _TASK_LOGON_TYPE.TASK_LOGON_GROUP;
td.Settings.Hidden = true;
// Add a trigger that will fire the task at this time every other day
IDailyTrigger dt = (IDailyTrigger)td.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_DAILY);
dt.StartBoundary = DateTime.Now.AddMinutes(10).ToString("yyyy-MM-ddThh:mm:ss-06:00");
dt.DaysInterval = 2; // Every other day
IExecAction action =(IExecAction) td.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC);
action.Path = @"C:\windows\notepad.exe";
// Register the task in the root folder
const string taskName = "TESTING Sync All Database";
ITaskFolder rootFolder = ts.GetFolder(@"\"); // Root Folder
rootFolder.RegisterTaskDefinition(taskName,td,(int)_TASK_CREATION.TASK_CREATE_OR_UPDATE, "MyUsrName","MyPassword",_TASK_LOGON_TYPE.TASK_LOGON_PASSWORD );
while (System.Runtime.InteropServices.Marshal.ReleaseComObject(rootFolder) != 0)
{
Application.DoEvents();
}
rootFolder = null;
while (System.Runtime.InteropServices.Marshal.ReleaseComObject(dt) != 0)
{
Application.DoEvents();
}
dt = null;
while (System.Runtime.InteropServices.Marshal.ReleaseComObject(action) != 0)
{
Application.DoEvents();
}
action = null;
while (System.Runtime.InteropServices.Marshal.ReleaseComObject(td) != 0)
{
Application.DoEvents();
}
td = null;
while (System.Runtime.InteropServices.Marshal.ReleaseComObject(ts) != 0)
{
Application.DoEvents();
}
ts = null;
//
// After all of the COM objects have been released and set to null, do the following:
GC.Collect(); // Start .NET CLR Garbage Collection
GC.WaitForPendingFinalizers(); // Wait for Garbage Collection to finish
}
这篇关于如何作为Windows任务计划程序计划任务运行时隐藏程序的窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文