示例#1
0
        /// <summary>
        /// Removes a Task Scheduler task from the PowerShell/ScheduledJobs folder
        /// based on a task name.
        /// </summary>
        /// <param name="taskName">Task Scheduler task name</param>
        /// <param name="force">Force running instances to stop and remove task</param>
        /// <param name="firstCheckForTask">First check for existence of task</param>
        public void RemoveTaskByName(
            string taskName,
            bool force,
            bool firstCheckForTask)
        {
            // Get registered task.
            IRegisteredTask iRegisteredTask = null;

            try
            {
                iRegisteredTask = _iRootFolder.GetTask(taskName);
            }
            catch (System.IO.DirectoryNotFoundException)
            {
                if (!firstCheckForTask)
                {
                    throw;
                }
            }
            catch (System.IO.FileNotFoundException)
            {
                if (!firstCheckForTask)
                {
                    throw;
                }
            }

            if (iRegisteredTask == null)
            {
                return;
            }

            // Check to see if any instances of this job/task is running.
            IRunningTaskCollection iRunningTasks = iRegisteredTask.GetInstances(0);

            if (iRunningTasks.Count > 0)
            {
                if (!force)
                {
                    string msg = StringUtil.Format(ScheduledJobErrorStrings.CannotRemoveTaskRunningInstance, taskName);
                    throw new ScheduledJobException(msg);
                }

                // Stop all running tasks.
                iRegisteredTask.Stop(0);
            }

            // Remove task.
            _iRootFolder.DeleteTask(taskName, 0);
        }
示例#2
0
        public void RemoveTaskByName(string taskName, bool force, bool firstCheckForTask)
        {
            IRegisteredTask task = null;

            try
            {
                task = this._iRootFolder.GetTask(taskName);
            }
            catch (DirectoryNotFoundException directoryNotFoundException)
            {
                if (!firstCheckForTask)
                {
                    throw;
                }
            }
            catch (FileNotFoundException fileNotFoundException)
            {
                if (!firstCheckForTask)
                {
                    throw;
                }
            }
            if (task != null)
            {
                IRunningTaskCollection instances = task.GetInstances(0);
                if (instances.Count > 0)
                {
                    if (force)
                    {
                        task.Stop(0);
                    }
                    else
                    {
                        string str = StringUtil.Format(ScheduledJobErrorStrings.CannotRemoveTaskRunningInstance, taskName);
                        throw new ScheduledJobException(str);
                    }
                }
                this._iRootFolder.DeleteTask(taskName, 0);
                return;
            }
            else
            {
                return;
            }
        }