C# 创建系统服务并定时执行【转载】
【转载】http://www.cnblogs.com/hfzsjz/archive/2011/01/07/1929898.html
C# 创建系统服务并定时执行
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 = ;
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 == && intMinute == && intSecond == ) ///定时设置,判断分时秒
{
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();
}
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
C# 创建系统服务并定时执行【转载】的更多相关文章
- linux创建定时任务,定时执行sql
终于弄清楚一个问题了.linux创建定时任务,定时执行sql,其中分为两个case. case-1 sql语句较少,因此直接在 shell脚本中 写sql语句.如下: [oracle@Oracle11 ...
- Windows任务计划创建计划,定时执行PowerShell命令
[环境介绍] 操作系统:Windows Server 2012 R2,64位操作系统 PowerShell版本:PowerShell 1.0 脚本位置:C:\BackUp.ps1 启动目录:C:\Wi ...
- 如何使用Linux的Crontab定时执行PHP脚本的方法[转载]
首先说说cron,它是一个linux下的定时执行工具.根用户以外的用户可以使用 crontab 工具来配置 cron 任务.所有用户定义的 crontab 都被保存在/var/spool/cron 目 ...
- mysql命令行创建存储过程命令行定时执行sql语句
mysql -uroot -p show databases; use scm; show tables; show procedure status; 其他命令: SHOW VARIABLES LI ...
- (转载)php中实现定时执行计划任务方法
(转载)http://www.111cn.net/phper/php/41216.htm PHP脚本执行时间限制,默认的是30m 解决办法:set_time_limit();或者修改PHP.ini 设 ...
- SQL sever 创建定时执行任务
在SQL的使用过程中,我们经常要做些数据备份以及定时执行的任务. 这些任务能够帮助我们简化工作过程. 下面我们了解下如何创建一个定时执行的存储过程. 首先我们要打开 SQL server 代理服务 选 ...
- kettle 创建任务定时执行数据抽取
定时执行脚本 使用SPOON 工具建立好转换文件 .ktr,创建下面的.BAT文件,用操作系统的任务调用批处理. G:\soft\data-integration\pan.bat /norep -fi ...
- 创建JOB定时执行存储过程
创建JOB定时执行存储过程有两种方式 方式1:通过plsql手动配置job,如下图: 方式2:通过sql语句,如下sql declare job_OpAutoDta pls_integer;--声明一 ...
- Java 在某一个时间点定时执行任务(转载)
java定时任务,每天定时执行任务.以下是这个例子的全部代码. public class TimerManager { //时间间隔 private static final long PERIOD_ ...
随机推荐
- 2014多校第二场1011 || HDU 4882 ZCC Loves Codefires (贪心)
题目链接 题意 : 给出n个问题,每个问题有两个参数,一个ei(所要耗费的时间),一个ki(能得到的score).每道问题需要耗费:(当前耗费的时间)*ki,问怎样组合问题的处理顺序可以使得耗费达到最 ...
- android模拟器(genymotion)+appium+python 框架执行基本原理(目前公司自己写的)
android模拟器(genymotion)+appium+python 框架执行的基本过程: 1.Push.initDate(openid)方法 //业务数据初始化 1.1 v5db.p ...
- jenkins忘记管理员账号密码的补救方法-转
源引自:http://www.cnblogs.com/xiami303/p/3625829.html 一不小心,忘记了admin用户的账号密码.然后就看不到manage jenkins的那部分内容了, ...
- JavaWeb笔记——上传文件
jsp上传文件 *<form>标签method属性必须为post,并且添加enctype="multipart/form-data"属性 ------------- ...
- HtmlAgilityPackage XPath学习
最近的开发中要用到htmlAgilityPackage, 所以记录一下XPath相关知识! XPath 简介 XPath 是一门在 XML 文档中查找信息的语言.XPath 可用来在 XML 文档中对 ...
- 转载网页博客:ie7bug:div容器下的img与div存在间隙
1.代码及在浏览器上的显示 ie7: ie8+: Firefox: Chrome: 可以看出ie7上在div容器下添加img,div与img中有间隙,而在ie8+和其他浏览器上均显示正常 网页源代码如 ...
- k近邻法
k近邻法(k nearest neighbor algorithm,k-NN)是机器学习中最基本的分类算法,在训练数据集中找到k个最近邻的实例,类别由这k个近邻中占最多的实例的类别来决定,当k=1时, ...
- highCharts图表入门实例
本文通过讲解Highcharts生成一个简单的3D柱状图实例来学习Highcharts的使用. JSP 页面 1.需要引入的js文件 <script src="<%=basePa ...
- ubuntu 12.10无法用apt-get安装软件 Err http://us.archive.ubuntu.com quantal-updates/main Sources 404 Not
之前执行apt-get 不管是什么软件或apt-get update都会遇到fail to fetch http://us.archive.ubuntu.com quantal-updates/ma ...
- adb 安卓opencv manager报错:adb server is out of date.killing
原因:ref:http://jingyan.baidu.com/article/d621e8da0dee022865913fce.html 最后发现360mobil.exe占用 5037 通 ...