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_ ...
随机推荐
- java基础知识回顾之抽象类
/* 抽象类: 抽象:笼统,模糊,看不懂!不具体. 特点: 1,方法只有声明没有实现时,该方法就是抽象方法,需要被abstract修饰. 抽象方法必须定义在抽象类中.该类必须也被abstract修饰. ...
- Visual Studio 常用快捷键 (二)
想不到上一篇 [Visual Studio 常用快捷键] 受这么多人的欢迎.看来大家对Visual Studio的用法非常感兴趣. 接下来我准备写一个 “Visual Studio使用技巧 ” 一个系 ...
- JDBC第一次学习
JDBC(Java Data Base Connectivity,java数据库连接),由一些类和接口构成的API,它是J2SE的一部分,由java.sql,javax.sql包组成. 应用程序.J ...
- delphi中的Label控件背景透明
Label1.Transparent:=true;你在它的属性窗口把它的Transparent属性改成TRUE就行了 来自为知笔记(Wiz)
- struts2与struts1整合,java.lang.InstantiationException, Exception occurred during processing request: null
做了2个action,其中一个运行没有问题,另一个报错,看下面的报错信息,再看了看struts.xml,因为没有给GetBooks这个action配置actionform,所以就导致报null.下面是 ...
- Iterator的remove方法可保证从源集合中安全地删除对象(转)
如果对正在被迭代的集合进行结构上的改变(即对该集合使用add.remove或clear方法),那么迭代器就不再合法(并且在其后使用该迭代器将会有ConcurrentModificationExcept ...
- c# 网络是否连接
c# 网络是否连接 方案一: using System; using System.Collections.Generic; using System.Linq; using System.Text ...
- marquee 笔记
页面的自动滚动效果,可由javascript来实现, 但是有一个html标签 - <marquee></marquee>可以实现多种滚动效果,无需js控制. 使用marquee ...
- java遍历Map时remove删除元素
public class T { /** * @param args */ public static void main(String[] args) { // TODO Auto-generate ...
- RPC简介及原理
简介 RPC(Remote Procedure Call,远程过程调用)是建立在Socket之上的,在一台机器上运行的主程序,可以调用另一台机器上准备好的子程序,就像LPC(本地过程调用). 越底层, ...