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


C# TaskService.Dispose方法代码示例

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


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

示例1: AddTask

        public void AddTask(int min, int hour, bool restart)
        {
            var taskService = new TaskService();

            //Create task definition
            var taskDefinition = taskService.NewTask();
            taskDefinition.RegistrationInfo.Description = min + "@" + hour + "@" + ((restart) ? "r" : "s");
            taskDefinition.Principal.UserId = "SYSTEM";

            var trigger = new DailyTrigger
            {
                StartBoundary = DateTime.Today + TimeSpan.FromHours(hour) + TimeSpan.FromMinutes(min)
            };

            taskDefinition.Triggers.Add(trigger);

            //Create task action
            var fileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"Power.exe");

            taskDefinition.Actions.Add(restart
                ? new ExecAction(fileName, "reboot \"This computer is going to reboot.\"")
                : new ExecAction(fileName, "shutdown \"This computer is going to shutdown to save power.\""));

            taskDefinition.Settings.AllowDemandStart = false;
            taskDefinition.Settings.DisallowStartIfOnBatteries = false;
            taskDefinition.Settings.DisallowStartOnRemoteAppSession = false;
            taskDefinition.Settings.StopIfGoingOnBatteries = false;

            taskService.RootFolder.RegisterTaskDefinition(@"FOG\" + taskDefinition.RegistrationInfo.Description,
                taskDefinition);
            taskService.Dispose();
        }
开发者ID:darksidemilk,项目名称:fog-client,代码行数:32,代码来源:WindowsGreen.cs

示例2: RemoveTask

 public void RemoveTask(int min, int hour, bool restart)
 {
     var taskService = new TaskService();
     var task = min + "@" + hour + "@" + ((restart) ? "r" : "s");
     taskService.RootFolder.DeleteTask(@"FOG\" + task);
     taskService.Dispose();
 }
开发者ID:darksidemilk,项目名称:fog-client,代码行数:7,代码来源:WindowsGreen.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: CleanTasks

        public static ActionResult CleanTasks(Session session)
        {
            try
            {
                var taskService = new TaskService();
                var existingTasks = taskService.GetFolder("FOG").AllTasks.ToList();

                foreach (var task in existingTasks)
                    taskService.RootFolder.DeleteTask(@"FOG\" + task.Name);

                taskService.RootFolder.DeleteFolder("FOG", false);
                taskService.Dispose();
            }
            catch (Exception)
            {
                // ignored
            }

            return ActionResult.Success;
        }
开发者ID:FOGProject,项目名称:fog-client,代码行数:20,代码来源:CustomAction.cs

示例5: Main

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            string dir = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
            string svc = Path.Combine(dir, "ConnectifyService.exe");
            string cli = Path.Combine(dir, "Connectify.exe");
            bool isService = false;
            if (File.Exists(svc) && File.Exists(cli)) {
                RegistryKey runKey = null;
                try {
                    // Remove the client autorun
                    runKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
                    runKey.DeleteValue("Connectify", false);

                } catch (Exception) {
                } finally {
                    if (runKey != null)
                        runKey.Close();
                }

                TaskService ts = null;
                try {
                    // Remove service scheduled task if it exists
                    ts = new TaskService();
                    foreach (Task t in ts.RootFolder.Tasks) {
                        if (t.Name.StartsWith("Connectify")) {
                            ts.RootFolder.DeleteTask(t.Name);
                            break;
                        }
                    }
                } catch (Exception) {
                } finally {
                    if (ts != null)
                        ts.Dispose();
                }

                ServiceController service = null;
                ManagementObject classInstance = null;
                ManagementBaseObject inParams = null;
                try {
                    // Set the service to run manually if it exists
                    service = new ServiceController("Connectify");
                    if (service != null) {
                        isService = true;
                        classInstance = new ManagementObject("root\\CIMV2", "Win32_Service.Name='Connectify'", null);
                        inParams = classInstance.GetMethodParameters("ChangeStartMode");
                        inParams["StartMode"] = "Manual";
                        classInstance.InvokeMethod("ChangeStartMode", inParams, null);
                    }
                } catch (Exception) {
                } finally {
                    if (inParams != null)
                        inParams.Dispose();
                    if (classInstance != null)
                        classInstance.Dispose();
                }

                try {
                    if (isService) {
                        // Stop the service if it happens to be running
                        service.Stop();
                    } else {
                        // Kill services if they happen to be running
                        foreach (Process p in Process.GetProcessesByName("ConnectifyService")) {
                            p.Kill();
                            p.WaitForExit();
                        }
                        foreach (Process p in Process.GetProcessesByName("Connectifyd")) {
                            p.Kill();
                            p.WaitForExit();
                        }
                    }
                } catch (Exception) {
                }

                Process daemon = null;
                try {
                    if (isService) {
                        // Start the service
                        service.Start();
                        service.WaitForStatus(ServiceControllerStatus.Running);
                    } else {
                        // Launch the service & client
                        daemon = Process.Start(svc);
                        daemon.WaitForInputIdle();
                    }
                } catch (Exception) {
                }

                // Launch the client
                Process client = Process.Start(cli);

                // Wait for client to quit
                client.WaitForExit();

                try {
                    if (isService) {
                        // Stop the service
//.........这里部分代码省略.........
开发者ID:bsiegel,项目名称:connectify-launcher,代码行数:101,代码来源:Program.cs

示例6: CloseTaskService

 /// <summary>
 /// 關閉連線
 /// </summary>        
 private static void CloseTaskService(TaskService ts)
 {
     ts.Dispose();
 }
开发者ID:hiehm,项目名称:test,代码行数:7,代码来源:TaskService.cs

示例7: ClearAll

        public void ClearAll()
        {
            var taskService = new TaskService();

            try
            {
                taskService.RootFolder.CreateFolder("FOG");
            }
            catch (Exception)
            {
                // ignored
            }

            var existingTasks = taskService.GetFolder("FOG").AllTasks.ToList();

            foreach (var task in existingTasks)
            {
                Log.Debug(LogName, "Delete task " + task.Name);
                taskService.RootFolder.DeleteTask(@"FOG\" + task.Name);
            }

            taskService.Dispose();
        }
开发者ID:darksidemilk,项目名称:fog-client,代码行数:23,代码来源:WindowsGreen.cs


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