使用.net 创建windows service
最近公司项目需要,写了个windows 服务,windows 服务的内容可以在VS 中新建一个“windows服务项目”,
(1)服务中的主要代码:
public partial class Service1 : ServiceBase {
public Service1()
{
InitializeComponent();
TaskToRun();
}
protected override void OnStart(string[] args) {
}
protected override void OnStop() {
}
private void TaskToRun() {
TimerCallback tcb = new TimerCallback(writeToLocal);
AutoResetEvent autoEvent = new AutoResetEvent(false);
// 每隔5分钟执行一次
System.Threading.Timer timer = new Timer(tcb, autoEvent, 10000, 60000 * 5);
}
private void writeToLocal(object objectstate)
{
string path = ConfigurationManager.AppSettings["savePath"].ToString();
using (StreamWriter writer = File.AppendText(path))
{ writer.WriteLine("程序执行时间:" + DateTime.Now.ToString());
}
}
}
(2)在service1 中右击点击“添加安装程序”:

(3)在ServiceIntall1 中编辑服务名称,并在ServiceProcessIntall1 中将“Account”属性修改为“LocalSystem”

(4)编译生成项目文件并将bin 文件部署到服务器上。
(5) 在要部署的服务器上安装installUtil.exe:
以我的机器(安装的是.net framework 4.0)为例,打开命令行,运行如下命令来安装服务:
cd c:\windows\Microsfot.Net\Framework\V4.0.30319
intallutil.exe D:\WinserviceTest1\WinserviceTest1.exe


(6) 这样WinserviceTest1 服务安装完成后,就可以在windows“服务”列表中找到该服务并启动:

(7)最后可以查看到该服务的运行结果:

(8)参考资料:
1)C#创建、安装一个Windows服务:
http://blog.csdn.net/yysyangyangyangshan/article/details/10515035
http://www.cnblogs.com/pingfanren/archive/2015/11/22/4986053.html
使用.net 创建windows service的更多相关文章
- C#创建Windows Service(Windows 服务)基础教程
Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...
- 创建Windows Service
基本参照使用C#创建Windows服务,添加了部分内容 目录 创建Windows Service 可视化管理Windows Service 调试 示例代码 创建Windows Service 选择C# ...
- C# 创建Windows Service(Windows服务)程序
本文介绍了如何用C#创建.安装.启动.监控.卸载简单的Windows Service 的内容步骤和注意事项. 一.创建一个Windows Service 1)创建Windows Service项目 2 ...
- .NET 6学习笔记(2)——通过Worker Service创建Windows Service
通过Visual Studio中的Windows Service模板,我么可以创建.NET Framework版本的Windows Service,网络上对此已有详细且丰富的各路教程.但在我们升级到. ...
- C# 创建Windows Service
当我们需要一个程序长期运行,但是不需要界面显示时可以考虑使用Windows Service来实现.这篇博客将简单介绍一下如何创建一个Windows Service,安装/卸载Windows Servi ...
- 目前.NET Core创建Windows Service比较好的一个开源框架:DasMulli.Win32.ServiceUtils
新建一个.NET Core控制台程序,搜索并下载Nuget包:DasMulli.Win32.ServiceUtils GitHub 链接及使用指南 Write a windows service us ...
- 通过TopShelf简单创建windows service
目前很多项目都是B/S架构的,我们经常会用到webapi.MVC等框架,实际项目中可能不仅仅是一些数据的增删改查,需要对数据进行计算,但是将计算逻辑放到api层又会拖累整个项目的运行速度,从而会写一些 ...
- 【C#】C#创建Windows Service服务
目录结构: contents structure [+] 创建Windows服务 配置 安装Windows服务 在Visual Studio中调试 常见问题 最近写了一个TCP连接的程序,由于这种通信 ...
- VS2010 创建 windows service 程序
参考网上保护眼睛程序,自写程序如下. 1.创建一个名词为“CareEyeService”,类型为“WindowsService”的应用程序. 自动生成代码如下图: 2.修改ServiceCareEye ...
随机推荐
- 三种renderman规范引擎的dice对比
次表面做的有些烦躁,既然如此,索性先记一下前一阵比较的PIXIE.3delight.prman的dice方式. 研究过reyes的人都知道dice,简而言之,就是为了生成高质量高精度的图片(电影CG) ...
- pip自动生成requirements.txt依赖关系清单
Python项目中经常会带requirements.txt文件,里面是项目所依赖的包的列表,也就是依赖关系清单,这个清单也可以使用pip命令自动生成. pip命令: 1 pip freeze > ...
- [BZOJ 3223 & Tyvj 1729]文艺平衡树 & [CodeVS 3243]区间翻转
题目不说了,就是区间翻转 传送门:BZOJ 3223 和 CodeVS 3243 第一道题中是1~n的区间翻转,而第二道题对于每个1~n还有一个附加值 实际上两道题的思路是一样的,第二题把值对应到位置 ...
- Keepalived双机热备
一,Keepalived双机热备的应用场景 1,网站流量不高,压力不大,但是对服务器的可靠性要求极其高,例如实时在线OA系统,政府部门网站系统,医院实时报医系统,公安局在线报案系统,股市后台网站系统等 ...
- Windows And Video Memory
MSDN Blogs > Zemblanity > Windows And Video Memory Windows And Video Memory Tom_Mulcahy 11 F ...
- ajax登陆提示
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Con ...
- win7系统下的FTP配置
2016-07-12 工作中需要在win7操作系统下配置FTP,遇到许多问题,所以记录下来方便以后解决问题. FTP是文件传输协议的简称.用于Internet上的控制文件的双向传输.同时,它也是一个应 ...
- LPTHW 笨办法学python 33章
32-33章节 将for-loop和while-loop循环的. 俩种句法就不说了.简单说下个人对于for和while的理解. 我觉得他learn python the hard way这里的写法是, ...
- CNN 逐层可视化 Ipython Notebook
http://nbviewer.ipython.org/github/BVLC/caffe/blob/master/examples/00-classification.ipynb
- Nothing about semantics
Motivation fork a project in github, seriously. Candidates PasaLab / cichlid 80% Distributed RDFS &a ...