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了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值