惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
M
MIT News - Artificial intelligence
D
Docker
量子位
T
Tailwind CSS Blog
人人都是产品经理
人人都是产品经理
月光博客
月光博客
有赞技术团队
有赞技术团队
J
Java Code Geeks
A
About on SuperTechFans
P
Proofpoint News Feed
Jina AI
Jina AI
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
Microsoft Security Blog
Microsoft Security Blog
V
V2EX
GbyAI
GbyAI
F
Fortinet All Blogs

博客园 - asheng

JavaScript UI选型及Jquery EasyUI使用经验谈 云计算风险识别 五年之痒 敏捷软件开发 嫁给程序员吧!!! Cute Editor for .NET v6.4 Java与.NET的WebServices相互调用 用户体验5大要素 【转】LINQ To XML 入门(3) 【转】LINQ To XML 入门(2) 【转】LINQ To XML 入门(1) 微软发布ASP.NET MVC框架1.0正式版 Web定时任务 CSS分类编写方法 JS中错误处理(2) JS中错误处理(1) Log4Net OWC画图表 - asheng - 博客园 好员工为什么会变坏
学写Windows Service
asheng · 2009-04-07 · via 博客园 - asheng

记得很早之前,一个同事要写一个定时任务,先要在一条新闻在到达设定期限时 由程序自动删除

可以一直没有思路 问我 我也没有好的办法

后来,我在前段时间找到一个 方法:http://www.cnblogs.com/ucetgg/archive/2009/02/11/1388481.html

也不太好,也有个高人回复了 给我指明了思路,用Windows Service ,可我没有写过

后来 看到一篇文章:http://www.cnblogs.com/lovecherry/archive/2005/03/25/125527.html

用Windows Service 作为关键词 搜了搜博客园 有很多人已经做过

步骤我就不详细说了 ,帖我的代码出来吧:

Code
namespace WindowsService
{
    
public partial class Service1 : ServiceBase
    {
        
public Service1()
        {
            InitializeComponent();
        }
/// <summary>
        
/// 开始
        
/// </summary>
        
/// <param name="args"></param>
        protected override void OnStart(string[] args)
        {
            
//Thread t = new Thread(new ThreadStart(Run));
            
//t.Start();
            timer1.Enabled = true;
            execTask();
            
        }
/// <summary>
        
/// 结束
        
/// </summary>
        protected override void OnStop()
        {
            timer1.Enabled 
= false;
        }
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            Thread t 
= new Thread(new ThreadStart(execTask));
            t.Start();
        }
        
/// <summary>
        
/// 执行任务
        
/// </summary>
        private void execTask()
        {
            
string strSql = "insert into WebSite_CMSContent (Title,body) values('tt','xx')";
            DbHelperSQL.ExecuteSql(strSql);
        }
    }
}

注意:

1.Timer控件 一定要是 System.Timers.Timer 类型

2. InstallUtil.exe 一定要对应framework 的版本,比如f ramework 2.0的程序 1.1版本下的InstallUtil工具就不行

3.默认情况下 服务是不自动启动的,需要安装成功后手动 启动

//****************************************
  by: Amen cnblogs博客  转载请注明出处
//****************************************