Ejemplo n.º 1
0
        private void create_Click(object sender, EventArgs e)
        {
            var jobName        = ((ListItem)taskComboBox.SelectedItem).Value;
            var jobDisplayName = (taskComboBox.SelectedItem as ListItem)?.Text;
            var startDateTime  = this.startDateTime.Value;
            var repeatInterval = Convert.ToInt32((repeatComboBox.SelectedItem as ListItem).Value);

            if (jobName == "ExternalExecute")
            {
                jobName = Guid.NewGuid().ToString();
            }

            try
            {
                var tasks = new Tasks();
                if (tasks.TaskExists(jobName))
                {
                    var dialogResult = MessageBox.Show($"Are you sure you want to overwrite the task {jobName}?", "Overwrite Task?", MessageBoxButtons.YesNo);
                    if (dialogResult == DialogResult.Yes)
                    {
                        if (JobManager.DeleteJob(jobName))
                        {
                            tasks.DeleteTask(jobName);
                        }
                    }
                }

                if (!JobManager.ScheduleJob(jobName, startDateTime, repeatInterval, txtExternalExecutable.Text, txtExternalParameters.Text))
                {
                    return;
                }

                MessageBox.Show(tasks.CreateTask(jobName, jobDisplayName, startDateTime, repeatInterval,
                                                 txtExternalExecutable.Text, txtExternalParameters.Text)
                    ? $"Task created: {jobName}"
                    : $@"Task {jobName} created but will be lost if application is closed.");

                Close();
                TaskManager.LoadTasks();
            }
            catch (ArgumentException ex)
            {
                Logger.Instance.Error($"An error occured while creating the task {jobName}: {ex}");
                MessageBox.Show($@"An error occured while creating the task {jobName}");
                TaskManager.LoadTasks();
            }
        }
Ejemplo n.º 2
0
        private void tasksGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var senderGrid = (DataGridView)sender;

            if (e.ColumnIndex == senderGrid.Columns["Pause"].Index &&
                senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
                e.RowIndex >= 0)
            {
                // Pause
                DataGridViewRow row     = senderGrid.CurrentCell.OwningRow;
                string          jobName = row.Cells[senderGrid.Columns["TaskName"].Index].Value.ToString();

                var job = JobManager.GetJob(jobName);
                var runningJobContexts       = JobManager.GetCurrentlyExecutingJobs();
                List <IJobDetail> pausedJobs = new List <IJobDetail>();
                foreach (IJobExecutionContext runningJobContext in runningJobContexts)
                {
                    if (JobManager.IsTriggerPaused(runningJobContext.Trigger.Key.Name, runningJobContext.Trigger.Key.Group))
                    {
                        pausedJobs.Add(runningJobContext.JobDetail);
                    }
                }

                if (pausedJobs.Contains(job))
                {
                    if (JobManager.ResumeJob(jobName))
                    {
                        MessageBox.Show($@"Task {jobName} resumed.", @"Emptied Successfully");
                    }
                    else
                    {
                        MessageBox.Show($@"Task {jobName} failed to resume.", @"Emptied Successfully");
                    }
                }
                else
                {
                    if (JobManager.PauseJob(jobName))
                    {
                        MessageBox.Show($@"Task {jobName} paused.", @"Emptied Successfully");
                    }
                    else
                    {
                        MessageBox.Show($@"Task {jobName} failed to pause.", @"Emptied Successfully");
                    }
                }


                LoadTasks();
            }

            if (e.ColumnIndex == senderGrid.Columns["Delete"].Index &&
                senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
                e.RowIndex >= 0)
            {
                try
                {
                    DataGridViewRow row     = senderGrid.CurrentCell.OwningRow;
                    string          jobName = row.Cells[senderGrid.Columns["TaskName"].Index].Value.ToString();
                    if (JobManager.DeleteJob(jobName))
                    {
                        var Tasks = new Tasks();
                        Tasks.DeleteTask(jobName);
                        MessageBox.Show($@"Task {jobName} deleted.", @"Emptied Successfully");
                    }
                    else
                    {
                        MessageBox.Show($@"Task {jobName} failed to delete.", @"Emptied Successfully");
                    }

                    LoadTasks();
                }
                catch (Exception)
                {
                    MessageBox.Show($@"An error occured while deleting the task.", @"Emptied Successfully");
                }
            }
        }