任务计划程序不会调用notepad.exe。 [英] Task Scheduler not calling notepad.exe.

查看:133
本文介绍了任务计划程序不会调用notepad.exe。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在通过任务计划程序课程调用notepad.exe时遇到困难。

另请提供给我一步一步开发应用程序..



非常感谢。

这里是代码:



 静态  void  Main( string  [] args)
{
使用(TaskService ts = new TaskService(
Environment.MachineName,
username
Environment.MachineName,
密码
false
))
{
// 创建新任务定义并分配属性
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = 做点什么;


DailyTrigger dTrigger =(DailyTrigger)td.Triggers.Add( new DailyTrigger());
dTrigger.StartBoundary = DateTime.Today + TimeSpan.FromHours( 19 );
dTrigger.DaysInterval = 1 ;

dTrigger.Repetition.Duration = TimeSpan.FromHours( 2 );
dTrigger.Repetition.Interval = TimeSpan.FromHours( 1 );

// 创建一个触发器,每隔一天在此时触发任务
td.Triggers.Add( new DailyTrigger {DaysInterval = 1 });
td.Triggers.Add( new DailyTrigger( 1 ));

// 创建一个在触发器触发时将启动记事本的操作

// string notepadexepath = @C:\ Windows \ System32 \\\ npad。 exe;
td.Actions.Add( new ExecAction( < span class =code-string> notepad.exe
null null ) );

// td.Actions.Add(new ExecAction(notepadexepath,null,null)) ;

// 在根文件夹中注册任务
ts.RootFolder.RegisterTaskDefinition( @ Test_Schdeular,td);
任务t = ts.RootFolder.RegisterTaskDefinition(
@ Test_Schdeular
td,
TaskCreation.CreateOrUpdate,
SYSTEM
null
TaskLogonType.InteractiveToken
);

// 删除我们刚创建的任务
// ts.RootFolder.DeleteTask(Test_Schdeular);
}

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault( false );
// Application.Run();
Application.Run( new Form1());
}

解决方案

这适用于Windows 8和Visual Studio 2012.此示例释放COM对象以防止内存问题。





添加对COM对象的引用:TaskScheduler.dll

 使用 TaskScheduler; 


...


public void CreateSchedule()
{
ITaskService ts = new TaskScheduler.TaskScheduler();
ts.Connect();
// 创建新任务定义并分配属性
ITaskDefinition td = ts .NewTask( 0 );
td.Settings.MultipleInstances = _TASK_INSTANCES_POLICY.TASK_INSTANCES_IGNORE_NEW;
td.RegistrationInfo.Description = 启动记事本;
td.Settings.Hidden = false ;
td.Settings.Priority = 4 ;
td.Settings.StartWhenAvailable = true ;
td.Principal.LogonType = _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN;

// 添加触发器,每隔一天在此时触发任务
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 ; // 每隔一天
IExecAction action =(IExecAction)td.Actions.Create(_TASK_ACTION_TYPE .TASK_ACTION_EXEC);
action.Path = @ C:\windows \\\
otepad.exe
;
action.Arguments = @ C:\ Windows\win.ini;
// 在根文件夹中注册任务
const string taskName = 记事本;
ITaskFolder rootFolder = ts.GetFolder( @ \); // 根文件夹
rootFolder.RegisterTaskDefinition(taskName,td,( int )_ TASK_CREATION.TASK_CREATE_OR_UPDATE, ,_ TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN);
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 ;
//
// 在释放所有COM对象并将其设置为null后,执行以下操作:
GC.Collect(); // 启动.NET CLR垃圾收集
GC.WaitForPendingFinalizers(); // 等待垃圾收集完成
}


Hi,

I am stuck at calling notepad.exe through Task Scheduler class.
Also please provide me the Step By Step to develop the application..

Many Thanks.
here is the code:

static void Main(string[] args)
{
    using (TaskService ts = new TaskService(
        Environment.MachineName,
        "username",
        Environment.MachineName,
        "Password",
        false
    ))
    {
        // Create a new task definition and assign properties
        TaskDefinition td = ts.NewTask();
        td.RegistrationInfo.Description = "Does something";


        DailyTrigger dTrigger = (DailyTrigger)td.Triggers.Add(new DailyTrigger());
        dTrigger.StartBoundary = DateTime.Today + TimeSpan.FromHours(19);
        dTrigger.DaysInterval = 1;

        dTrigger.Repetition.Duration = TimeSpan.FromHours(2);
        dTrigger.Repetition.Interval = TimeSpan.FromHours(1);

        // Create a trigger that will fire the task at this time every other day
        td.Triggers.Add(new DailyTrigger { DaysInterval = 1 });
        td.Triggers.Add(new DailyTrigger(1));

        // Create an action that will launch Notepad whenever the trigger fires

        // string notepadexepath = @"C:\Windows\System32\notepad.exe";
        td.Actions.Add(new ExecAction("notepad.exe", null, null));

        // td.Actions.Add(new ExecAction(notepadexepath, null,null));

        // Register the task in the root folder
        ts.RootFolder.RegisterTaskDefinition(@"Test_Schdeular", td);
        Task t = ts.RootFolder.RegisterTaskDefinition(
            @"Test_Schdeular",
            td,
            TaskCreation.CreateOrUpdate,
            "SYSTEM",
            null,
            TaskLogonType.InteractiveToken
        );

        // Remove the task we just created
        //ts.RootFolder.DeleteTask("Test_Schdeular");
    }

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    //Application.Run();
    Application.Run(new Form1());
}

解决方案

This works for me on Windows 8 with Visual Studio 2012. This example releases the COM objects to prevent memory issues.


Add a Reference to COM object: TaskScheduler.dll

using TaskScheduler;


...


        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 = "Starting Notepad";
            td.Settings.Hidden = false;
            td.Settings.Priority = 4;
            td.Settings.StartWhenAvailable = true;
            td.Principal.LogonType = _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN;
           
            // 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";
            action.Arguments = @"C:\Windows\win.ini";
            // Register the task in the root folder
            const string taskName = "Notepad";
            ITaskFolder rootFolder = ts.GetFolder(@"\"); // Root Folder
            rootFolder.RegisterTaskDefinition(taskName, td, (int)_TASK_CREATION.TASK_CREATE_OR_UPDATE, "", "", _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN);
            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
        }


这篇关于任务计划程序不会调用notepad.exe。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆