【转载】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# 创建系统服务并定时执行【转载】的更多相关文章

  1. linux创建定时任务,定时执行sql

    终于弄清楚一个问题了.linux创建定时任务,定时执行sql,其中分为两个case. case-1 sql语句较少,因此直接在 shell脚本中 写sql语句.如下: [oracle@Oracle11 ...

  2. Windows任务计划创建计划,定时执行PowerShell命令

    [环境介绍] 操作系统:Windows Server 2012 R2,64位操作系统 PowerShell版本:PowerShell 1.0 脚本位置:C:\BackUp.ps1 启动目录:C:\Wi ...

  3. 如何使用Linux的Crontab定时执行PHP脚本的方法[转载]

    首先说说cron,它是一个linux下的定时执行工具.根用户以外的用户可以使用 crontab 工具来配置 cron 任务.所有用户定义的 crontab 都被保存在/var/spool/cron 目 ...

  4. mysql命令行创建存储过程命令行定时执行sql语句

    mysql -uroot -p show databases; use scm; show tables; show procedure status; 其他命令: SHOW VARIABLES LI ...

  5. (转载)php中实现定时执行计划任务方法

    (转载)http://www.111cn.net/phper/php/41216.htm PHP脚本执行时间限制,默认的是30m 解决办法:set_time_limit();或者修改PHP.ini 设 ...

  6. SQL sever 创建定时执行任务

    在SQL的使用过程中,我们经常要做些数据备份以及定时执行的任务. 这些任务能够帮助我们简化工作过程. 下面我们了解下如何创建一个定时执行的存储过程. 首先我们要打开 SQL server 代理服务 选 ...

  7. kettle 创建任务定时执行数据抽取

    定时执行脚本 使用SPOON 工具建立好转换文件 .ktr,创建下面的.BAT文件,用操作系统的任务调用批处理. G:\soft\data-integration\pan.bat /norep -fi ...

  8. 创建JOB定时执行存储过程

    创建JOB定时执行存储过程有两种方式 方式1:通过plsql手动配置job,如下图: 方式2:通过sql语句,如下sql declare job_OpAutoDta pls_integer;--声明一 ...

  9. Java 在某一个时间点定时执行任务(转载)

    java定时任务,每天定时执行任务.以下是这个例子的全部代码. public class TimerManager { //时间间隔 private static final long PERIOD_ ...

随机推荐

  1. Difference Between Initialization and Assignment in C++

    Initialization happens when a variable is given a value at the moment it is created. Assignment obli ...

  2. 深入浅出ES6(十二):代理 Proxies

    作者 Jason Orendorff  github主页  https://github.com/jorendorff 请看这样一段代码: var obj = new Proxy({}, { get: ...

  3. ExtJs布局之Column

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...

  4. 三、Android NDK编程预备之Java jni入门创建C/C++共享库

    转自: http://www.eoeandroid.com/thread-264971-1-1.html 应网友回复,答应在两天前要出一篇创建C/C++共享库的,但由于清明节假期,跟朋友出去游玩,丢手 ...

  5. Unit4中的Annotation

    Unit4中的Annotation(注解.注释) JUnit4 使用 Java 5 中的注解(annotation),以下是JUnit4 常用的几个annotation介绍@Before:初始化方法@ ...

  6. QTP10.0安装说明

    QTP10.0 安装手册 注:安装之前检查清理相关注册表:运行->regdit-HKEY_LOCAL_MACHINE->HKEY_LOCAL_MACHINE\SOFTWARE->HK ...

  7. struts2实现选择i18n语言选择切换

    [新手学习记录,仅供参考!] 1.项目准备 首先当然是我们得创建一个struts2的web项目,并且已经实现了一个简单的功能. 以下通过登录功能来举例说明. 2.指定全局国际化资源文件 在struts ...

  8. hdu 3537 Daizhenyang's Coin (翻硬币游戏)

    #include<stdio.h> #include<algorithm> #include<string.h> using namespace std; ]; i ...

  9. AngularJs+bootstrap搭载前台框架——准备工作

    1.关于什么是AngularJs以及什么是bootstrap我就不多说了,简单说下,AngularJs是一个比较强大前台MVC框架,bootstrap是Twitter推出的一个用于前端开发的开源工具包 ...

  10. scala函数式编程

    1.作为值的函数 在Scala中,函数和数字一样,可以在变量中存放函数.可以把函数赋值给一个变量,格式为:val foee=fun _(函数名+空格+_)形式 2.匿名函数 在scala中,不需要给每 ...