好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

c#计划任务

c#计划任务

做一个类似计划任务的模块,在配置文件里写要执行的时间,让程序定时执行。

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

学习网址

Asp.net:

http://book.chinaz.com/others/web/web/aspnet/index.htm

Data Access Application Block for .NET

http://www.microsoft.com/china/MSDN/library/EnterpriseDevelopment/BuildDistApp/Vsdnbdadaab_rm.mspx?mfr=true

  开源资料大全

俺要读的书

Programming C#中文版:第4版

ASP.NET 2.0揭秘(卷2): http://book.csdn.net/bookfiles/488/

最优化ASP.NET (面向对象): http://book.csdn.net/bookfiles/90/index.html

精通基于ASP.NET 2.0的Web 2.0应用(如RSS、Blog、Tags、Web service、BBS、XML、AJAX、WIKI) :

                                             http://book.csdn.net/bookfiles/425/index.html

Expert C# 2005 Business Objects中文版 : http://book.csdn.net/bookfiles/397/

Beginning C# Objects从概念到代码 : http://book.csdn.net/bookfiles/26/index.html

C#和.NET实战:平台、语言与框架 : http://book.csdn.net/bookfiles/588/index.html

框架设计(第2版):CLR Via C# : http://book.csdn.net/bookfiles/154/index.html

C#高级编程(第4版) : http://book.csdn.net/bookfiles/140/

Effective C#中文版:改善C#程序的50种方法 : http://book.csdn.net/bookfiles/295/

ASP.NET 2.0服务器控件与组件开发高级编程 : http://book.csdn.net/bookfiles/337/index.html

 道不远人:深入解析ASP.NET 2.0控件开发 : http://book.csdn.net/bookfiles/533/


作者: Leo_wl

    

出处: http://www.cnblogs.com/Leo_wl/

    

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

版权信息

查看更多关于c#计划任务的详细内容...

  阅读:71次