【转载】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. HDU 2059 龟兔赛跑(动态规划)

    龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  2. QTP菜单消失的解决办法

    解决办法一:点击QTP上方菜单栏“Tools” menu->options 项,点击"General" tab, 最后点击“Restore Layout”按钮. 解决办法二: ...

  3. iOS开发--提交github代码

    将的SampleTable例子提交到github 具体步骤如下: a. 首先登陆github b. 创建新的reponsitory name, description c. 打开terminal, c ...

  4. Spring框架学习 - 配置

    [资料] ★★☆ Spring 中提供一些Aware相关接口,像是BeanFactoryAware. ApplicationContextAware.ResourceLoaderAware.Servl ...

  5. java异常——RuntimeException和User Define Exception

    1.RuntimeException public class RuntimeException { public static void main(String[] args) { // TODO ...

  6. USACO Section 2.2: Subset Sums

    dp题,一碰到dp我基本就是跪,搜了网上的答案分两种,一维和二维. 先讲二维,sum[i][j]表示前i个数的subset里差值为j的分法数量.当加入数字i时,有两种选择,某一个set和另外一个set ...

  7. 纯HTML标签详解

    HTML标签很多,可是实际上常用的却就那么十几二十个,很多标签的功能渐渐的被大家忽略了.然后,如果在适当的时候,用一用,还是能在一定程序上 给我们的页面设计带来一点小小的方便的.下面这些HTML标签基 ...

  8. 下拉刷新控件(3)系统自带的下拉刷新控件SwipeRefreshLayout(推荐*)

    1,简介 The SwipeRefreshLayout should be used whenever the user can refresh the contents of a view via ...

  9. wget 批量下载目录文件

    wget -r -p -k -np http://源目录     ./本地目标目录

  10. MDM平台学习笔记

    最近和将来一段时间都会花很多时间在主数据管理平台的学习和开发上,从现在开始打算记录此过程中的知识点和学习心得,加油! 1.IBM全新的产品文档网站IBM Knowledge Center,软件硬件产品 ...