基本信息
源码名称:定时自动运行任务
源码大小:0.07M
文件格式:.rar
开发语言:C#
更新时间:2017-09-05
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

在数据库中设置好定时时间,及运行的程序,及参数

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AutoLoadSynctoy
{
    public partial class frmMain : Form
    {

        Dal dal = new Dal();
        List<AutoTask> lstRun = null;
        bool bRun = false;

        public frmMain()
        {
            InitializeComponent();
        }

        private void btnLoadTask_Click(object sender, EventArgs e)
        {
            lstRun = dal.gettaskList();
            dataGridView1.DataSource = lstRun;
            textBox1.AppendText(DateTime.Now.ToString("yyyyMMdd HH:mm:ss") " 加载任务列表" Environment.NewLine);
        }

        private void frmMain_Load(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int Hour = DateTime.Now.Hour;
            int Minute = DateTime.Now.Minute;
            int Second = DateTime.Now.Second;

            foreach (AutoTask ta in lstRun)
            {
                if (Hour == ta.tHour
                    && Minute == ta.tMinute
                    && Second == ta.tSecond)
                {
                    try
                    {
                        timer1.Enabled = false;
                        textBox1.AppendText(DateTime.Now.ToString("yyyyMMdd HH:mm:ss") " 任务开始,名称:" ta.taskName Environment.NewLine);
                        LogCls.Log("任务开始,名称:" ta.taskName);
                        StartProcess(ta.AppPath, ta.AppParm);

                        textBox1.AppendText(DateTime.Now.ToString("yyyyMMdd HH:mm:ss") " 任务完成,名称:" ta.taskName Environment.NewLine);
                        LogCls.Log("任务完成");
                    }
                    catch (Exception ex)
                    {
                        LogCls.Log("task1任务错误", "原因:" ex.Message);
                        System.Threading.Thread.Sleep(1000);
                    }
                    finally
                    {
                        timer1.Enabled = true;
                    }
                }

            }
        }

        public bool StartProcess(string filename, string args)
        {
            Process myprocess = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo(filename, args);
            myprocess.StartInfo = startInfo;

            //通过以下参数可以控制exe的启动方式,具体参照 myprocess.StartInfo.下面的参数,如以无界面方式启动exe等
            myprocess.StartInfo.UseShellExecute = false;
            myprocess.Start();
            myprocess.WaitForExit();

            return true;
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            if (btnRun.Text == "运行")
            {
                if (lstRun.Count == 0)
                {
                    MessageBox.Show("无任务");
                    return;
                }

                timer1.Enabled = true;
                btnRun.Text = "停止";
                textBox1.AppendText(DateTime.Now.ToString("yyyyMMdd HH:mm:ss") " 开始监视任务" Environment.NewLine);
            }
            else
            {
                timer1.Enabled = false;
                btnRun.Text = "运行";
                textBox1.AppendText(DateTime.Now.ToString("yyyyMMdd HH:mm:ss") " 停止监视任务" Environment.NewLine);
            }
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Visible = true;
            this.ShowInTaskbar = true;
            this.WindowState = FormWindowState.Normal;
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (timer1.Enabled)
            {
                if (e.CloseReason == CloseReason.UserClosing)//当用户点击窗体右上角X按钮或(Alt F4)时 发生          
                {
                    e.Cancel = true;
                    this.ShowInTaskbar = false;
                    this.notifyIcon1.Icon = this.Icon;
                    this.Hide();
                }
            }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}