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

推荐订阅源

N
News and Events Feed by Topic
WordPress大学
WordPress大学
Vercel News
Vercel News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
小众软件
小众软件
L
LangChain Blog
雷峰网
雷峰网
D
DataBreaches.Net
博客园 - 三生石上(FineUI控件)
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tor Project blog
NISL@THU
NISL@THU
Scott Helme
Scott Helme
量子位
S
Security Affairs
T
Threat Research - Cisco Blogs
博客园_首页
云风的 BLOG
云风的 BLOG
D
Docker
AWS News Blog
AWS News Blog
腾讯CDC
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
U
Unit 42
Recent Announcements
Recent Announcements
Apple Machine Learning Research
Apple Machine Learning Research
G
Google Developers Blog
T
The Exploit Database - CXSecurity.com
MongoDB | Blog
MongoDB | Blog
Stack Overflow Blog
Stack Overflow Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 热门话题
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The Last Watchdog
The Last Watchdog
C
Cybersecurity and Infrastructure Security Agency CISA
IT之家
IT之家
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
F
Full Disclosure
L
Lohrmann on Cybersecurity
The Hacker News
The Hacker News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Y
Y Combinator Blog
S
Security @ Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
C
Check Point Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
N
News and Events Feed by Topic
PCI Perspectives
PCI Perspectives
I
InfoQ

博客园 - 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博客  转载请注明出处
//****************************************