.net 定时执行 windows 服务
1.新建项目 --》 Windows 服务 2.Service1.cs代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.IO;
using System.Text;
using System.Timers;
using System.Data.SqlClient;
using System.Threading;
namespace InnPoint
{  
public partial class Service : ServiceBase
    {  
public Service()
        {  
InitializeComponent();
}
protected override void OnStart(string[] args)
        {  
            EventLog.WriteEntry("我的服务启动");//在系统事件查看器里的应用程序事件里来源的描述  
            writestr("服务启动");//自定义文本日志  
System.Timers.Timer t = new System.Timers.Timer();
t.Interval = 1000;
t.Elapsed += new System.Timers.ElapsedEventHandler(ChkSrv);//到达时间的时候执行事件;
t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;
}
/// <summary>
/// 定时检查,并执行方法
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
public void ChkSrv(object source, System.Timers.ElapsedEventArgs e)
        {  
int intHour = e.SignalTime.Hour;
int intMinute = e.SignalTime.Minute;
int intSecond = e.SignalTime.Second;
if (intHour == 13 && intMinute == 30 && intSecond == 00) ///定时设置,判断分时秒
            {  
try
                {  
System.Timers.Timer tt = (System.Timers.Timer)source;
tt.Enabled = false;
SetInnPoint();
tt.Enabled = true;
}
catch (Exception err)
                {  
writestr(err.Message);
}
}
}
//我的方法
public void SetInnPoint()
        {  
try
            {  
                writestr("服务运行");  
//这里执行你的东西
Thread.Sleep(10000);
}
catch (Exception err)
            {  
writestr(err.Message);
}
}
///在指定时间过后执行指定的表达式
///
///事件之间经过的时间(以毫秒为单位)
///要执行的表达式
public static void SetTimeout(double interval, Action action)
        {  
System.Timers.Timer timer = new System.Timers.Timer(interval);
timer.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e)
            {  
timer.Enabled = false;
action();
};
timer.Enabled = true;
}
public void writestr(string readme)
        {  
//debug==================================================
            //StreamWriter dout = new StreamWriter(@"c:/" + System.DateTime.Now.ToString("yyyMMddHHmmss") + ".txt");  
StreamWriter dout = new StreamWriter(@"c:/" + "WServ_InnPointLog.txt", true);
            dout.Write("/r/n事件:" + readme + "/r/n操作时间:" + System.DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"));  
//debug==================================================
dout.Close();
}
protected override void OnStop()
        {  
            writestr("服务停止");  
            EventLog.WriteEntry("我的服务停止");  
}
}
}
3.在Service1.cs设计页面右键添加安装程序
4.ProjectInstaller.cs设计页面中
serviceInstaller1属性中设置:
Description(系统服务的描述)
DisplayName (系统服务中显示的名称)
ServiceName(系统事件查看器里的应用程序事件中来源名称)
serviceProcessInstaller1属性设置:
Account 下拉设置成 LocalSystem
5.在.net Framework的安装目录可以找到InstallUtil.exe,可以复制出来放在你的服务生成的exe一起
6.安装服务setup.bat批处理文件内容:InstallUtil Service1.exe ,安装好后可以在系统服务中找到你设置的服务名称然后可以启动这个服务
7.卸载服务UnInstall.bat批处理文件内容:InstallUtil -u Service1.exe
转自:http://blog.csdn.net/windxxf/article/details/6014538
.net 定时执行 windows 服务的更多相关文章
- windows 服务实现定时任务调度(Quartz.Net)
		我们通常在一些情况下需要软件具有一个自动执行某些任务的功能,但是又不希望直接启动软件,或者每次都要手动的来启动软件,这时我们可可以考虑到windows服务了. 首先创建一个windows服务项目(详细 ... 
- linux centos7 定时执行服务监控脚本
		2021-08-25 1. 需求 在服务挂掉之后我们要怎么做才能保证服务在短时间内开启?可以编写脚本监控服务的状态,在服务挂掉后及时将其开启,并定时执行该脚本. 2. 脚本编写 思路:平常我们可以通过 ... 
- Centos7 利用crontab定时执行任务及配置方法
		crond是什么? crond 和crontab是不可分割的.crontab是一个命令,常见于Unix和类Unix的操作系统之中,用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于 ... 
- linux下使用crontab定时执行脚本
		使用crontab定时执行脚本 cron服务是一个定时执行的服务,可以通过crontab 命令添加或者编辑需要定时执行的任务: crontab –e : 修改 crontab 文件,如果文件不存在会自 ... 
- 使用 Topshelf 结合 Quartz.NET 创建 Windows 服务
		Ø 前言 之前一篇文章已经介绍了,如何使用 Topshelf 创建 Windows 服务.当时提到还缺少一个任务调度框架,就是 Quartz.NET.而本文就展开对 Quartz.NET 的研究,以 ... 
- 快速搭建多线程Windows服务解决方案
		一.引言 在软件开发过程中windows服务有的时候非常有用,用于同步数据,发送邮件,宿主WF引擎服务等,但是快速搭建一个好用多线程进行多任务处理的程序往往是一个项目必须考虑的问题.自己在项目中也经常 ... 
- C#创建windows服务并定时执行
		一.创建window服务 1.新建项目-->选择Windows服务.默认生成文件包括Program.cs,Service1.cs 2.在Service1.cs添加如下代码: System.Tim ... 
- .net 开发定时执行的windows服务
		环境:win7+vs2010+Oracle11g+office2010(64位操作系统) 需求:开发定时执行的windows服务从数据库中查询数据下载到指定地址Excel中 一.添加新建项目——win ... 
- C#  编写windows服务及服务的安装、启动、删除、定时执行任务
		一.编写windows服务 1.VS2017 - 创建服务Myservice 2.创建好项目之后 --- >> 双击 Service1.cs ---- >> 出现一个设计 ... 
随机推荐
- [luogu_P1251][LOJ#6008]「网络流 24 题」餐巾计划
			[luogu_P1251][LOJ#6008]「网络流 24 题」餐巾计划 试题描述 一个餐厅在相继的 \(N\) 天里,第 \(i\) 天需要 \(R_i\) 块餐巾 \((i=l,2,-,N)\) ... 
- Chrome 浏览器访问 Google 学术出现问题 “but your computer or network may be sending automated queries. ”
			问题: Chrome 浏览器访问 Google 学术出现如下的问题 : ... but your computer or network may be sending automated querie ... 
- linux下头文件
			aio.h 异步I/Oassert.h 验证程序断言complex 复数类complex.h 复数处理cpio.h cpio归档值ctype.h 字符类型dirent.h 目录项,opendir(), ... 
- ubuntu  配置虚拟主机
			ubuntu下Apache虚拟主机的配置 启用站点停用站点方法 a2ensite/a2dissite 比如添加一个ecshop 的虚拟机 首先到/etc/apache2/site-availa ... 
- Named Formats!
			原文发布时间为:2011-06-26 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Text;using System.Web;using Sys ... 
- 8个学习.net的博客链接 (以前收藏过更多的,被百度新版搞没了,恨死了)
			原文发布时间为:2012-09-18 -- 来源于本人的百度文章 [由搬家工具导入] Simone Chiaretta’s CodeClimber http://www.haacked.com/ ( ... 
- javascript草稿
			原文发布时间为:2011-06-01 -- 来源于本人的百度文章 [由搬家工具导入] @MyHelper.Script("jquery-1.6.1.min.js", Url) ... 
- HTML title属性换行显示的方法
			原文发布时间为:2009-04-22 -- 来源于本人的百度文章 [由搬家工具导入] 解决的方法有两种: 1.将title属性分成几行来写,例如:<a href=#" title=&q ... 
- Shell中的单引号(‘)双引号(”)和反引号(·)
			在bash中,$.*.?.[.].’.”.`.\.有特殊的含义.类似于编译器的预编译过程,bash在扫描命令行的过程中,会在文本层次上,优先解释所有的特殊字符,之后对转换完成的新命令行,进行内核的系统 ... 
- Scrapy学习-25-Scrapyd部署spider
			Scrapyd部署爬虫项目 github项目 https://github.com/scrapy/scrapyd 官方文档 http://scrapyd.readthedocs.org/ ... 
