Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a task using C# code

C#
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);
            }
        }


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.
Posted
Updated 14-Jun-13 8:00am
v2

1 solution

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
}
 
Share this answer
 
v5
Comments
lalit.mca2006 13-Jun-13 20:46pm    
Thanks Mike ,I tried it but its not working in my case . still its showing that command window popup .
Mike Meinz 14-Jun-13 11:43am    
In my testing on Windows 8, I found that when "Run only when user is logged on" is checked, the Hidden attribute is ignored. When I set "Run whether user is logged on or not", the Hidden attribute was honored and the task was not visible.

I updated the solution with my code.

lalit.mca2006 18-Jun-13 0:26am    
thanks Mike ,for this solution but actually i am using window server 2008 R2 and VS- 2008.
ts.Connect(); In this method 4 arguments are required , one of them is password . i am not able to capture password of window then how should i run . Please let me know .
Mike Meinz 18-Jun-13 6:06am    
Sorry, I do not have access to a Windows Server to do testing.

I looked at the documentation for TaskService Connect, it says that the username/password are optional.

If you need the username/password, can you prompt your user to enter the password at the time that you are creating the Task Scheduler entry?
lalit.mca2006 19-Jun-13 6:31am    
Thanks Mike to write the solution ,but actually it is not possible in my case because i have
separate exe which is calling inside main application .Do you have any other idea to do so ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900