当前位置: 首页>>代码示例>>C#>>正文


C# TaskService.AddTask方法代码示例

本文整理汇总了C#中Microsoft.Win32.TaskScheduler.TaskService.AddTask方法的典型用法代码示例。如果您正苦于以下问题:C# TaskService.AddTask方法的具体用法?C# TaskService.AddTask怎么用?C# TaskService.AddTask使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.Win32.TaskScheduler.TaskService的用法示例。


在下文中一共展示了TaskService.AddTask方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AddStores2TaskScheduler

 public static void AddStores2TaskScheduler(string strStoresPath, string strActionPath)
 {
     string[] strXMLFiles = Directory.GetFiles(strStoresPath, "*.xml");
     TaskService ts = new TaskService();
     Console.ForegroundColor = ConsoleColor.Cyan;
     Console.WriteLine("");
     Console.WriteLine("Adding stores to the Task Scheduler");
     Console.ForegroundColor = ConsoleColor.Green;
     foreach (string strXMLFile in strXMLFiles)
     {
         string storeName = Path.GetFileName(strXMLFile);
         string taskName = @"BC Store " + storeName;
         Task t = ts.FindTask(taskName);
         if (t == null)
         {
             Console.WriteLine("  + " + storeName);
             DailyTrigger dt = new DailyTrigger();
             dt.StartBoundary = DateTime.Today.Date;
             dt.Repetition.Duration = TimeSpan.FromMinutes(1430);
             dt.Repetition.Interval = TimeSpan.FromMinutes(2);
             ts.AddTask(taskName, dt, new ExecAction(strActionPath, strXMLFile, null));
             Thread.Sleep(500);
         }
     }
     Console.ForegroundColor = ConsoleColor.Cyan;
     Console.WriteLine("All stores added");
     Console.WriteLine("");
     Console.WriteLine("");
     Console.ForegroundColor = ConsoleColor.White;
 }
开发者ID:rogerlio,项目名称:csharp-proj,代码行数:30,代码来源:Utils.cs

示例2: CreateTask

        public void CreateTask(TaskService service)
        {
            var path = Application.StartupPath;

            try
            {
                var task = service.AddTask("SP_Field_Monitor",
                                           new DailyTrigger() { StartBoundary = DateTime.Parse("10:00:00 AM") },
                                           new ExecAction(path + "\\SPFieldMonitor.exe",
                                                          "-user [email protected] -password \"!thisisatestitisonlyatest!\"",
                                                          path), "SYSTEM");

                var trigger = new DailyTrigger() { StartBoundary = DateTime.Parse("11:00:00 AM") };
                trigger.Repetition.Duration = TimeSpan.FromHours(22);
                trigger.Repetition.Interval = TimeSpan.FromHours(1);

                var checkTask = service.AddTask("SP_Field_Monitor_Check", trigger,
                                           new ExecAction(path + "\\SPFieldMonitor.exe",
                                                          "-user [email protected] -password \"!thisisatestitisonlyatest!\" -check",
                                                          path), "SYSTEM");

                MessageBox.Show(
                    "The daily task has been scheduled.\nIf you move any of the Field Monitor executables, you will need to click this button again to reschedule the task.",
                    "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show(
                    "You need administrator privileges to schedule this task. Try running the program as an administrator.",
                    "Insufficient Access Rights", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
开发者ID:jwarnes,项目名称:backupMon,代码行数:32,代码来源:frmMain.cs

示例3: PersistTask

        public static bool PersistTask(bool startAtLogon, bool startForAllUsers)
        {
            WinTasks.TaskService ts = new WinTasks.TaskService();
            try
            {
                WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
                bool isElevated = (new WindowsPrincipal(currentIdentity).IsInRole(WindowsBuiltInRole.Administrator));
                WinTasks.Task task = ts.FindTask(Application.ProductName, false);

                if (startAtLogon)
                {
                    if (startForAllUsers && !isElevated)
                    {
                        return PersistTaskElevated(startAtLogon, startForAllUsers);
                    }
                    else
                    {
                        WinTasks.LogonTrigger trigger = (WinTasks.LogonTrigger)WinTasks.LogonTrigger.CreateTrigger(WinTasks.TaskTriggerType.Logon);
                        trigger.Enabled = true;
                        trigger.StartBoundary = DateTime.Today;
                        if (startForAllUsers)
                            trigger.UserId = null;
                        else
                            trigger.UserId = currentIdentity.Name;

                        WinTasks.ExecAction action = (WinTasks.ExecAction)WinTasks.ExecAction.CreateAction(WinTasks.TaskActionType.Execute);
                        action.Path = Application.ExecutablePath;
                        action.WorkingDirectory = System.IO.Path.GetDirectoryName(Application.ExecutablePath);

                        if (task == null)
                        {
                            task = ts.AddTask(Application.ProductName, trigger, action, currentIdentity.Name);
                        }
                        else
                        {
                            task.Definition.Triggers.Clear();
                            task.Definition.Triggers.Add(trigger);
                            task.Definition.Actions.Clear();
                            task.Definition.Actions.Add(action);
                            task.RegisterChanges();
                        }
                    }
                }
                else if (task != null)
                {
                    ts.GetFolder("\\").DeleteTask(task.Name);
                }
            }
            catch
            {
                return false;
            }
            finally
            {
                ts.Dispose();
            }
            return true;
        }
开发者ID:Mrunited12,项目名称:realtemp-4-rtss,代码行数:58,代码来源:TaskPersistance.cs

示例4: LongTest

        internal static void LongTest(TaskService ts, System.IO.TextWriter output, params string[] arg)
        {
            string user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

            Version ver = ts.HighestSupportedVersion;
            bool isV12 = (ver >= new Version(1, 2));
            bool isV13 = (ver >= new Version(1, 3));
            bool isV14 = (ver >= new Version(1, 4));
            output.WriteLine("Highest version: " + ver);
            output.WriteLine("Server: {0} ({1}); User: {2}\\{3}", ts.TargetServer, ts.Connected ? "Connected" : "Disconnected", ts.UserAccountDomain, ts.UserName);

            output.WriteLine("Running tasks:");
            foreach (RunningTask rt in ts.GetRunningTasks(true))
            {
                if (rt != null)
                {
                    output.WriteLine("+ {0}, {1} ({2})", rt.Name, rt.Path, rt.State);
                    if (ver.Minor > 0)
                        output.WriteLine("  Current Action: " + rt.CurrentAction);
                }
            }

            string filter = arg.Length > 0 ? arg[0] : string.Empty;
            TaskFolder tf = ts.RootFolder;
            TaskCollection tasks = tf.GetTasks(new Wildcard(filter));
            output.WriteLine("\nRoot folder tasks matching \"{1}\" ({0}):", tasks.Count, filter);
            foreach (Task t in tasks)
            {
                try
                {
                    output.WriteLine("+ {0}, {1} ({2}) - {3}", t.Name, t.Definition.RegistrationInfo.Author, t.State, t.Definition.Settings.Compatibility);
                    foreach (Trigger trg in t.Definition.Triggers)
                        output.WriteLine(" + {0}", trg);
                    foreach (var act in t.Definition.Actions)
                        output.WriteLine(" = {0}", act);
                }
                catch { }
            }

            output.WriteLine("\n***Finding defrag task***");
            Task ft = ts.FindTask("*defrag*");
            if (ft != null)
                output.WriteLine("Defrag task found at " + ft.Path);
            else
                output.WriteLine("Defrag task not found.");

            TaskFolderCollection tfs = tf.SubFolders;
            if (tfs.Count > 0)
            {
                output.WriteLine("\nSub folders:");
                try
                {
                    foreach (TaskFolder sf in tfs)
                        output.WriteLine("+ {0}", sf.Path);
                }
                catch (Exception ex)
                {
                    output.WriteLine(ex.ToString());
                }
            }

            if (isV12)
            {
                output.WriteLine("\n***Checking folder retrieval***");
                try
                {
                    const string testFolder = "David's TestFolder";
                    try { tf.CreateFolder(testFolder); }
                    catch (System.Runtime.InteropServices.COMException cex) { if (cex.ErrorCode != -2147024713) throw; }
                    catch { throw; }
                    TaskFolder sub = tf.SubFolders[testFolder];
                    output.WriteLine("\nSubfolder path: " + sub.Path);
                    try
                    {
                        ts.AddTask(testFolder + @"\MyTask", new DailyTrigger(), new ExecAction("notepad"));
                        output.WriteLine(" - Tasks: " + sub.Tasks.Count.ToString());
                        sub.DeleteTask("MyTask");
                    }
                    catch (Exception ex)
                    {
                        output.WriteLine(ex.ToString());
                    }
                    tf.DeleteFolder(testFolder);
                }
                catch (NotSupportedException) { }
                catch (Exception ex)
                {
                    output.WriteLine(ex.ToString());
                }
            }

            output.WriteLine("\n***Checking task creation***");
            try
            {
                TaskDefinition td = ts.NewTask();
                td.Data = "Your data";
                //td.Principal.UserId = "SYSTEM";
                //td.Principal.LogonType = TaskLogonType.ServiceAccount;
                if (isV12)
                    td.Principal.LogonType = TaskLogonType.S4U;
//.........这里部分代码省略.........
开发者ID:tablesmit,项目名称:task-scheduler-managed-wrapper,代码行数:101,代码来源:Program.cs


注:本文中的Microsoft.Win32.TaskScheduler.TaskService.AddTask方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。