예제 #1
0
        private void Prox_Ejec()
        {
            string         nombreApp = (Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)).Replace("Config.exe", "");
            ScheduledTasks Tareas    = new ScheduledTasks();

            TaskScheduler.Task tarea = Tareas.OpenTask(nombreApp);
            if (chkInicioW.Checked && tarea != null)
            {
                string proxima = tarea.NextRunTime.ToString();
                label18.Visible  = true;
                label18.Text     = ("Proxima ejecucion: " + proxima);
                chkEdit.Visible  = true;
                TimePicker.Value = Convert.ToDateTime(proxima);
                tarea.Close();

                Dehab();
            }
            else
            {
                label18.Visible    = false;
                label18.Text       = "";
                chkEdit.Visible    = false;
                chkInicioW.Checked = false;
            }
            chkEdit.Checked = false;
            Tareas.Dispose();
        }
예제 #2
0
        /// <summary>Schedule FileWall to start on logon.</summary>
        private static void ScheduleStartup(string assemblyPath)
        {
            //Get a ScheduledTasks object for the local computer.
            var scheduledTasks = new ScheduledTasks();

            scheduledTasks.DeleteTask("FileWall Launch");

            // Create a task
            var task = scheduledTasks.CreateTask("FileWall Launch");

            // Fill in the program info
            task.ApplicationName = assemblyPath;
            task.Comment         = "Launches FileWall when user logs on.";

            task.Flags |= TaskFlags.RunOnlyIfLoggedOn;

            // Set the account under which the task should run.
            task.SetAccountInformation(Environment.ExpandEnvironmentVariables("%USERNAME%"), (string)null);

            // Create a trigger to start the task every Sunday at 6:30 AM.
            task.Triggers.Add(new OnLogonTrigger());

            // Save the changes that have been made.
            task.Save();
            // Close the task to release its COM resources.
            task.Close();
            // Dispose the ScheduledTasks to release its COM resources.
            scheduledTasks.Dispose();
        }
예제 #3
0
        private static void ScheduleAfterInstall(string assemblyPath)
        {
            const string taskName    = "FileWall after install launch";
            const string taskComment = "Launches FileWall after install.";

            //Get a ScheduledTasks object for the local computer.
            var scheduledTasks = new ScheduledTasks();

            scheduledTasks.DeleteTask(taskName);

            // Create a task
            var task = scheduledTasks.CreateTask(taskName);

            // Fill in the program info
            task.ApplicationName = assemblyPath;
            task.Comment         = taskComment;

            task.Flags |= TaskFlags.RunOnlyIfLoggedOn | TaskFlags.DeleteWhenDone;

            // Set the account under which the task should run.
            task.SetAccountInformation(Environment.ExpandEnvironmentVariables("%USERNAME%"), (string)null);

            // Create a trigger to start the task every Sunday at 6:30 AM.
            task.Triggers.Add(new RunOnceTrigger(DateTime.Now));

            // Save the changes that have been made.
            task.Save();

            task.Run();

            // Close the task to release its COM resources.
            task.Close();
            // Dispose the ScheduledTasks to release its COM resources.
            scheduledTasks.Dispose();
        }
        /// <summary>
        /// Releases allocated resources.
        /// </summary>
        /// <param name="disposing">A value indicating if the method was called by user code. If <see langword="false"/>, the method was called by the runtime in the finalizer.</param>
        /// <remarks>If <paramref name="disposing"/> is <see langword="false"/>, no other objects should be referenced.</remarks>
        protected virtual void Dispose(bool disposing)
        {
            if (IsDisposed)
            {
                return;
            }

            try
            {
                if (disposing)
                {
                    if (mIsEnabled)
                    {
                        Stop();
                    }
                    if (TriggerTimer != null)
                    {
                        TriggerTimer.Dispose();
                    }
                    if (mTasks != null)
                    {
                        foreach (ScheduledTask <T> task in mTasks)
                        {
                            task.Dispose();
                        }
                        mTasks.Clear();
                    }
                    if (ScheduledTasks != null)
                    {
                        ScheduledTasks.Dispose();
                    }
                }
            }
            finally
            {
                IsDisposed = true;
            }
            TriggerTimer   = null;
            mTasks         = null;
            ScheduledTasks = null;
        }
예제 #5
0
        private bool IniciarWindows()
        {
            string         nombreApp = (Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)).Replace("Config.exe", "");
            ScheduledTasks Tareas    = new ScheduledTasks();

            string[] Existen = Tareas.GetTaskNames();
            short    hora    = Convert.ToInt16(TimePicker.Text.Substring(0, 2));
            short    min     = Convert.ToInt16(TimePicker.Text.Substring(3, 2));
            string   pass    = null;

            if (chkInicioW.Checked == true)
            {
                try
                {
                    //Referencia https://www.codeproject.com/Articles/2407/A-New-Task-Scheduler-Class-Library-for-NET

                    if (!Existen.Contains(nombreApp + ".job"))
                    {
                        TaskScheduler.Task tarea = Tareas.CreateTask(nombreApp);
                        tarea.ApplicationName = txtRutaRead.Text;
                        tarea.Comment         = "Tarea para ejecutar el ReadFiles diariamente";
                        tarea.SetAccountInformation(Environment.UserName, pass);
                        tarea.Creator  = Environment.UserName;
                        tarea.Priority = System.Diagnostics.ProcessPriorityClass.Normal;
                        tarea.Triggers.Add(new DailyTrigger(hora, min));
                        tarea.Flags = TaskFlags.RunOnlyIfLoggedOn;
                        tarea.Save();
                        tarea.Close();
                    }
                    else if (chkEdit.Checked)
                    {
                        TaskScheduler.Task tarea = Tareas.OpenTask(nombreApp);
                        tarea.Triggers.RemoveAt(0);
                        tarea.Triggers.Add(new DailyTrigger(hora, min));
                        tarea.Save();
                        tarea.Close();
                    }
                    Tareas.Dispose();
                    Prox_Ejec();
                    txtPassWind.Text = "";
                    return(true);
                }
                catch (Exception e)
                {
                    Tareas.DeleteTask(nombreApp);
                    Tareas.Dispose();
                    MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Prox_Ejec();
                    return(false);
                }
            }
            else
            {
                try
                {
                    Tareas.DeleteTask(nombreApp);
                    Tareas.Dispose();
                    Prox_Ejec();
                    return(true);
                }
                catch (Exception e)
                {
                    Prox_Ejec();
                    return(false);
                }
            }
        }
        //Đồng Ý
        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if ((getNameScheduled() != "") && (getTimeStart() != ""))
                {
                    if (MessageBox.Show("Bạn Có Chắc Chắn Muốn Thêm Một Lịch Trình Mới Không", "Time Manager", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        string strTimeStart = getTimeStart();
                        st = new ScheduledTasks(global.getNameComputer());
                        try
                        {
                            st.DeleteTask(frmTimeManager.strNameScheduled);
                        }
                        catch { }
                        t = st.CreateTask(getNameScheduled());
                        if (txtNameApplication.Text != "")
                        {
                            t.ApplicationName = getNameApplication();
                            t.SetAccountInformation(frmSetAccount.strUserName, frmSetAccount.strPassWord);
                        }
                        t.Flags = TaskFlags.SystemRequired;
                        short shortHourStart = Convert.ToInt16(global.getHour(getTimeStart()));

                        #region Hàng Ngày
                        //Hàng Ngày
                        if (cbLichTrinh.Text == "Hằng Ngày")
                        {
                            if (global.getPeriod(strTimeStart) == "PM")
                            {
                                shortHourStart = Convert.ToInt16(shortHourStart + 12);
                            }
                            t.Triggers.Add(new DailyTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), Convert.ToInt16(dtpDaily.Text)));
                            t.Save();
                            st.Dispose();
                            t.Dispose();
                        }
                        #endregion

                        #region Hàng Tuần
                        //Hàng Tuần
                        if (cbLichTrinh.Text == "Hằng Tuần")
                        {
                            if (cbMondayWeekly.Checked)
                            {
                                t.Triggers.Add(new WeeklyTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), DaysOfTheWeek.Monday));
                            }
                            if (cbTuesdayWeekly.Checked)
                            {
                                t.Triggers.Add(new WeeklyTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), DaysOfTheWeek.Tuesday));
                            }
                            if (cbWednesdayWeekly.Checked)
                            {
                                t.Triggers.Add(new WeeklyTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), DaysOfTheWeek.Wednesday));
                            }
                            if (cbThursdayWeekly.Checked)
                            {
                                t.Triggers.Add(new WeeklyTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), DaysOfTheWeek.Thursday));
                            }
                            if (cbFridayWeekly.Checked)
                            {
                                t.Triggers.Add(new WeeklyTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), DaysOfTheWeek.Friday));
                            }
                            if (cbSaturdayWeekly.Checked)
                            {
                                t.Triggers.Add(new WeeklyTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), DaysOfTheWeek.Saturday));
                            }
                            if (cbSundayWeekly.Checked)
                            {
                                t.Triggers.Add(new WeeklyTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), DaysOfTheWeek.Sunday));
                            }
                            if (!(cbMondayWeekly.Checked) && !(cbTuesdayWeekly.Checked) && !(cbWednesdayWeekly.Checked) &&
                                !(cbThursdayWeekly.Checked) && !(cbFridayWeekly.Checked) && !(cbSaturdayWeekly.Checked) && !(cbSundayWeekly.Checked))
                            {
                                MessageBox.Show("Bạn Chưa Chọn Bất Kỳ Ngày Nào Trong Tuần", "Time Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                            else
                            {
                                t.Save();
                            }
                        }
                        #endregion

                        #region Hàng Tháng

                        if (cbLichTrinh.Text == "Hằng Tháng")
                        {
                            if (cbNgayThuMonthly.Checked)
                            {
                                int[] intArray = { Convert.ToInt32(dtpMonthly.Text.Trim()) };
                                if (frmSelectMonth.boolCheck1)
                                {
                                    t.Triggers.Add(new MonthlyTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), intArray, MonthsOfTheYear.January));
                                }
                                if (frmSelectMonth.boolCheck2)
                                {
                                    t.Triggers.Add(new MonthlyTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), intArray, MonthsOfTheYear.February));
                                }
                                if (frmSelectMonth.boolCheck3)
                                {
                                    t.Triggers.Add(new MonthlyTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), intArray, MonthsOfTheYear.March));
                                }
                                if (frmSelectMonth.boolCheck4)
                                {
                                    t.Triggers.Add(new MonthlyTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), intArray, MonthsOfTheYear.April));
                                }
                                if (frmSelectMonth.boolCheck5)
                                {
                                    t.Triggers.Add(new MonthlyTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), intArray, MonthsOfTheYear.May));
                                }
                                if (frmSelectMonth.boolCheck6)
                                {
                                    t.Triggers.Add(new MonthlyTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), intArray, MonthsOfTheYear.June));
                                }
                                if (frmSelectMonth.boolCheck7)
                                {
                                    t.Triggers.Add(new MonthlyTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), intArray, MonthsOfTheYear.July));
                                }
                                if (frmSelectMonth.boolCheck8)
                                {
                                    t.Triggers.Add(new MonthlyTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), intArray, MonthsOfTheYear.August));
                                }
                                if (frmSelectMonth.boolCheck9)
                                {
                                    t.Triggers.Add(new MonthlyTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), intArray, MonthsOfTheYear.September));
                                }
                                if (frmSelectMonth.boolCheck10)
                                {
                                    t.Triggers.Add(new MonthlyTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), intArray, MonthsOfTheYear.October));
                                }
                                if (frmSelectMonth.boolCheck11)
                                {
                                    t.Triggers.Add(new MonthlyTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), intArray, MonthsOfTheYear.November));
                                }
                                if (frmSelectMonth.boolCheck12)
                                {
                                    t.Triggers.Add(new MonthlyTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), intArray, MonthsOfTheYear.December));
                                }
                                t.Save();
                            }
                            if (cbCuaThangMonthly.Checked)
                            {
                                if (frmSelectMonth.boolCheck1)
                                {
                                    t.Triggers.Add(new TaskScheduler.MonthlyDOWTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), getDaysOfTheWeek_Monthly(), getWhichWeek_Monthly(), MonthsOfTheYear.January));
                                }
                                if (frmSelectMonth.boolCheck2)
                                {
                                    t.Triggers.Add(new TaskScheduler.MonthlyDOWTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), getDaysOfTheWeek_Monthly(), getWhichWeek_Monthly(), MonthsOfTheYear.February));
                                }
                                if (frmSelectMonth.boolCheck3)
                                {
                                    t.Triggers.Add(new TaskScheduler.MonthlyDOWTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), getDaysOfTheWeek_Monthly(), getWhichWeek_Monthly(), MonthsOfTheYear.March));
                                }
                                if (frmSelectMonth.boolCheck4)
                                {
                                    t.Triggers.Add(new TaskScheduler.MonthlyDOWTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), getDaysOfTheWeek_Monthly(), getWhichWeek_Monthly(), MonthsOfTheYear.April));
                                }
                                if (frmSelectMonth.boolCheck5)
                                {
                                    t.Triggers.Add(new TaskScheduler.MonthlyDOWTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), getDaysOfTheWeek_Monthly(), getWhichWeek_Monthly(), MonthsOfTheYear.May));
                                }
                                if (frmSelectMonth.boolCheck6)
                                {
                                    t.Triggers.Add(new TaskScheduler.MonthlyDOWTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), getDaysOfTheWeek_Monthly(), getWhichWeek_Monthly(), MonthsOfTheYear.June));
                                }
                                if (frmSelectMonth.boolCheck7)
                                {
                                    t.Triggers.Add(new TaskScheduler.MonthlyDOWTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), getDaysOfTheWeek_Monthly(), getWhichWeek_Monthly(), MonthsOfTheYear.November));
                                }
                                if (frmSelectMonth.boolCheck8)
                                {
                                    t.Triggers.Add(new TaskScheduler.MonthlyDOWTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), getDaysOfTheWeek_Monthly(), getWhichWeek_Monthly(), MonthsOfTheYear.December));
                                }
                                if (frmSelectMonth.boolCheck9)
                                {
                                    t.Triggers.Add(new TaskScheduler.MonthlyDOWTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), getDaysOfTheWeek_Monthly(), getWhichWeek_Monthly(), MonthsOfTheYear.September));
                                }
                                if (frmSelectMonth.boolCheck10)
                                {
                                    t.Triggers.Add(new TaskScheduler.MonthlyDOWTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), getDaysOfTheWeek_Monthly(), getWhichWeek_Monthly(), MonthsOfTheYear.October));
                                }
                                if (frmSelectMonth.boolCheck11)
                                {
                                    t.Triggers.Add(new TaskScheduler.MonthlyDOWTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), getDaysOfTheWeek_Monthly(), getWhichWeek_Monthly(), MonthsOfTheYear.May));
                                }
                                if (frmSelectMonth.boolCheck12)
                                {
                                    t.Triggers.Add(new TaskScheduler.MonthlyDOWTrigger(shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), getDaysOfTheWeek_Monthly(), getWhichWeek_Monthly(), MonthsOfTheYear.June));
                                }
                                t.Save();
                            }
                        }

                        #endregion

                        #region Một Lần
                        if (cbLichTrinh.Text == "Một Lần")
                        {
                            int intGetDay   = Convert.ToInt32(global.getDay(dtpOnce.Text.Trim()));
                            int intGetMonth = Convert.ToInt32(global.getMonth(dtpOnce.Text.Trim()));
                            int intGetYear  = Convert.ToInt32(global.getYear(dtpOnce.Text.Trim()));
                            t.Triggers.Add(new TaskScheduler.RunOnceTrigger(new DateTime(intGetYear, intGetMonth, intGetDay, shortHourStart, Convert.ToInt16(global.getMinute(getTimeStart())), 0)));
                            t.Save();
                        }
                        #endregion
                        #region Lúc Hệ Thống Bắt Đầu + Logon  + Không Làm Việc
                        if (cbLichTrinh.Text == "Lúc Bằt Đầu Hệ Thống")
                        {
                            t.Triggers.Add(new TaskScheduler.OnSystemStartTrigger());
                            t.Save();
                        }

                        if (cbLichTrinh.Text == "Lúc Log On")
                        {
                            t.Triggers.Add(new TaskScheduler.OnLogonTrigger());
                            t.Save();
                        }

                        if (cbLichTrinh.Text == "Khi Không Làm Việc")
                        {
                            t.Triggers.Add(new TaskScheduler.OnIdleTrigger());
                            //SetScreenSaverTimeout(Convert.ToInt32(txtIdle.Text.Trim()) * 60);
                            t.Save();
                        }

                        #endregion
                        //t.Save();
                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show("Bạn Chưa Chọn Tên Công Việc Hoặc Giờ Bắt Đầu", "Time Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Time Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }