zoukankan      html  css  js  c++  java
  • c# (.net)计划任务

     刚刚去了一家公司面试,他们让我上机做一个类似计划任务的模块,在配置文件里写要执行的时间,让程序定时执行。

    1.建立配置文件App.config

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <!--设定每月执行计划任务的日期,先设定每月的16号,17号,25号执行-->
        <add key ="DateNum" value ="16,17,25"/>
      </appSettings>
    </configuration>

    2. 建立PlanWork.cs文件

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Configuration;
    using System.Timers;

    namespace PlanWork
    {
        public class PlanWork
        {
            static void Main(string[] args)
            {
                Myplan mp = new Myplan();


                //*************************************************
                //设定间隔时间是15天,测试的时候设定时间为1000纳秒
                //Timer t = new Timer(15 * 24 * 60 * 60000);
                Timer t = new Timer(1000);
                //*************************************************


                //绑定定时触发的函数
                t.Elapsed += new ElapsedEventHandler(mp.RunMyplan);
                t.Start();
                Console.ReadLine();
            }

        }
        public class Myplan
        {
            public void RunMyplan(Object source, ElapsedEventArgs e)
            {
                //读取配置文件设定的日期时间
                string SetDate = ConfigurationManager.AppSettings["DateNum"].ToString();

                //获取现在的系统时间
                DateTime nowTime = System.DateTime.Now;
                string d = nowTime.Day.ToString(); //取日期

                //比较是否符合设定的时间,SetDate中是否有d的存在
                int i = SetDate.IndexOf(d);
                if (i >= 0)
                {
                    //计划任务要执行程序
                    Console.Write("\nToday is " + d + " day!");
                }

            }

        }
    }

    这样一个计划任务的小程序就ok了。

    作者:罗敏贵
    邮箱:minguiluo@163.com
    QQ群:34178394 建群 主要是寻找志同道合的人士一起学习和讨论自己的所学所思
    出处:http://luomingui.cnblogs.com/
    说明:专注于微软平台项目架构、熟悉设计模式、架构设计、敏捷个人和项目管理。现主要从事WinForm、ASP.NET、等方面的项目开发、架构、管理工作。文章为作者平时里的思考和练习,可能有不当之处,请博客园的园友们多提宝贵意见。
    知识共享许可协议本作品采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可。

  • 相关阅读:
    hexo及next主题修改
    LeetCode#476 Number Complement
    Html学习笔记(二) 简单标签
    Haproxy的应用
    STM32 一个初始化EXTI的例子
    sql语句优化原则
    在Docker中运行gocd
    Gnome Ubuntu16安装Nvidia显卡396驱动,CUDA9.2以及cudnn9.2
    吴裕雄--天生自然数据结构:十大经典排序算法——希尔排序
    吴裕雄--天生自然数据结构:十大经典排序算法——插入排序
  • 原文地址:https://www.cnblogs.com/luomingui/p/1665973.html
Copyright © 2011-2022 走看看