C#创建windows服务并定时执行
一、创建window服务
1、新建项目-->选择Windows服务。默认生成文件包括Program.cs,Service1.cs
2、在Service1.cs添加如下代码:
System.Timers.Timer timer1; //计时器
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args) //服务启动执行
{
timer1 = new System.Timers.Timer();
timer1.Interval = 3000; //设置计时器事件间隔执行时间
timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);
timer1.Enabled = true;
}
protected override void OnStop() //服务停止执行
{
this.timer1.Enabled = false;
}
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//执行SQL语句或其他操作
}
二、添加window服务安装程序
1、打开Service1.cs【设计】页面,点击右键,选择【添加安装程序】,会出现serviceInstaller1和serviceProcessInstaller1两个组件
2、将serviceProcessInstaller1的Account属性设为【LocalSystem】, serviceInstaller1的StartType属性设为【Automatic】,ServiceName属性可设置服务名称,此后在【管理工 具】--》【服务】中即显示此名称
3、ProjectInstaller.cs文件,在安装服务后一般还需手动启动(即使上述StartType属性设为【Automatic】),可在ProjectInstaller.cs添加如下代码实现安装后自动启动
public ProjectInstaller()
{
InitializeComponent();
this.Committed += new InstallEventHandler(ProjectInstaller_Committed);
}
private void ProjectInstaller_Committed(object sender, InstallEventArgs e)
{
//参数为服务的名字
System.ServiceProcess.ServiceController controller = new System.ServiceProcess.ServiceController("服务名称");
controller.Start();
}
三、安装、卸载window服务
1、输入cmd(命令行),输入cd C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319,2.0为cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
2、安装服务(项目生成的exe文件路径)
InstallUtil "E:\WindowsService1\bin\Debug\WindowsService1.exe"
3、卸载服务
InstallUtil /u "E:\WindowsService1\bin\Debug\WindowsService1.exe"
四、查看window服务
控制面板-->管理工具-->服务,可在此手动启动,停止服务
五、调试window服务
1、通过【事件查看器】查看
2、直接在程序中调试(菜单-->调试-->附加进程-->服务名(这里的服务名是项目名称,不是ServiceName属性自定义的名称,所以建议自定义名称和项目名称保持一致,另外需勾选【显示所有用户的进程】才能看到服务名)-->附加
3. 在程序中打断点调试即可,另外调试服务时服务必须已启动(管理工具-->服务)
文字转自:http://blog.csdn.net/armyfai/article/details/8056976
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
C#创建windows服务并定时执行的更多相关文章
- C# 创建Windows服务。服务功能:定时操作数据库 (转)
C# 创建Windows服务.服务功能:定时操作数据库 一.创建window服务 1.新建项目-->选择Windows服务.默认生成文件包括Program.cs,Service1.cs 2.在S ...
- vs 2010创建Windows服务定时timer程序
vs 2010创建Windows服务定时timer程序: 版权声明:本文为搜集借鉴各类文章的原创文章,转载请注明出处: http://www.cnblogs.com/2186009311CFF/p/ ...
- 用C#创建Windows服务(Windows Services)
用C#创建Windows服务(Windows Services) 学习: 第一步:创建服务框架 创建一个新的 Windows 服务项目,可以从Visual C# 工程中选取 Windows 服务(W ...
- (转)C#创建windows服务
原文地址:http://blog.itpub.net/23109131/viewspace-688117/ 第一步:创建服务框架 创建一个新的 Windows 服务项目,可以从Visual C# 工程 ...
- C#创建Windows服务与安装-图解
1.创建windows服务项目
- VS2013创建Windows服务 || VS2015+Windows服务简易教程
转自:https://www.cnblogs.com/no27/p/4849123.htmlhttps://blog.csdn.net/ly416/article/details/78860522 V ...
- 使用 Topshelf 创建 Windows 服务
Ø 前言 C# 创建 Windows 服务的方式有很多种,Topshelf 就是其中一种方式,而且使用起来比较简单.下面使用 Visual Studio Ultimate 2013 演示一下具体的使 ...
- asp.net基于windows服务实现定时发送邮件的方法
本文实例讲述了asp.net基于windows服务实现定时发送邮件的方法.分享给大家供大家参考,具体如下: //定义组件 private System.Timers.Timer time; publi ...
- 使用Topshelf组件 一步一步创建 Windows 服务
我们先来介绍一下使用它的好处,以下论述参考自其他大神. topshelf是创建windows服务的一种方式,相比原生实现ServiceBase.Install.Installer更为简单方便, 我们只 ...
随机推荐
- Jenkins pipeline 入门到精通系列文章
Jenkins2 入门到精通系列文章. Jenkins2 下载与启动jenkins2 插件安装jenkins2 hellopipelinejenkins2 pipeline介绍jenkins2 jav ...
- 使用SharePoint CSOM 编写高效的程序
上一篇文章中简单的介绍了使用CSOM进行编程.今天主要讲一下CSOM使用中一些小技巧,可以让你的程序运行的更快. 单独加载某些属性 在上文中的例子,需要返回Web对象信息的时候,我们使用了如下的代码: ...
- Hibernate各种主键生成器策略与配置详解(转载)
http://www.cnblogs.com/kakafra/archive/2012/09/16/2687569.html 1.assigned 主键由外部程序负责生成,在 save() 之前必须指 ...
- 电影成生活O2O必争之地,破局之战就此拉开
这一次的两会过后,互联网最流行的一个词恐怕当属“互联网+”.尤其是总理关于“以互联网为载体.把线上线下互动的新兴消费搞得红红火火”的一席话,更是让国内的O2O从业者兴奋不已.百度李彦宏在两会接受记者采 ...
- 让delphi解析chrome扩展的native应用
chrome浏览器自从去年以来逐步去掉了对浏览器插件的支持,npapi的方案马上不可用. 当务之急要选择一个替代方案,最常用的就是扩展了.扩展程序提供了一套和本地程序交互的方案——“原生消息通信” 写 ...
- Leetcode 257 Binary Tree Paths 二叉树 DFS
找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...
- 解决用navicate远程连接数据库出现1045 access denied for user 'root'@'localhost' using password yes
在mysql命令行中执行 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456'); GRANT ALL PRIVILEGES ON *.* ...
- unity 读取excel表 生成asset资源文件
做unity 项目也有一段时间了,从unity项目开发和学习中也遇到了很多坑,并且也从中学习到了很多曾经未接触的领域.项目中的很多功能模块,从今天开始把自己的思路和代码奉上给学渣们作为一份学习的资料. ...
- javascript设计模式与开发实践阅读笔记(8)——观察者模式
发布-订阅模式,也叫观察者模式:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都将得到通知. 在JavaScript开发中,我们一般用事件模型来替代传统的观察者模式. ...
- android: 播放音频
在 Android 中播放音频文件一般都是使用 MediaPlayer 类来实现的,它对多种格式的音 频文件提供了非常全面的控制方法,从而使得播放音乐的工作变得十分简单.下表列出了 MediaPlay ...