C#使用Windows Service
前言:Microsoft Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序。这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面。这种服务非常适合在服务器上使用,或任何时候,为了不影响在同一台计算机上工作的其他用户,需要长时间运行功能时使用。还可以在不同于登录用户的特定用户帐户或默认计算机帐户的安全上下文中运行服务。
- C#创建Windows服务的步骤:
1.创建Windows Service项目
从Visual C# 工程中选取 Windows 服务(Windows Service)选项,给工程一个新文件名,然后点击 确定。
2.向服务中函数功能实现
OnStart函数在启动服务时执行,OnStop函数在停止服务时执行。在这里,当启动和停止服务时,向一个文本文件中写入一些文字信息,代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks; namespace MyService
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
} protected override void OnStart(string[] args)
{
FileStream fs = new FileStream(@"d:\xx.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine("WindowsService: Service Started" + DateTime.Now.ToString() + "\n"); sw.Flush();
sw.Close();
fs.Close();
} //protected override void OnContinue()
//{
// base.OnContinue();
//} //protected override void OnPause()
//{
// base.OnPause(); // father class method inherit
//} //protected override void OnShutdown()
//{
// base.OnShutdown();
//} protected override void OnStop()
{
FileStream fs = new FileStream(@"d:\xx.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine("WindowsService: Service Stopped" + DateTime.Now.ToString() + "\n");
sw.Flush();
sw.Close();
fs.Close(); }
}
}
4.回到设计窗口点右键选择-添加安装程序 -生成serviceInstaller1和 serviceProcessInstaller1两个组件
把serviceInstaller1的属性ServiceName改写为你的服务程序名,并把启动模 式设置为AUTOMATIC
把serviceProcessInstaller1的属性account改写为 LocalSystem
5.编译链接生成服务程序
通过从生成菜单中选择生成来生成项目。
6.安装服务
以管理员的身份运行cmd

7.卸载服务
用项目的输出作为参数,从命令行运行 InstallUtil.exe。
installutil /u yourproject.exe

你可以在代码编写当中,写入timer定时器,开启服务之后就可以在服务器端不停的跑代码了。例如执行sqlserver数据库存储过程。

C#使用Windows Service的更多相关文章
- C#创建、安装、卸载、调试Windows Service(Windows 服务)的简单教程
前言:Microsoft Windows 服务能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面.这 ...
- 如何利用mono把.net windows service程序迁移到linux上
How to migrate a .NET Windows Service application to Linux using mono? 写在最前:之所以用要把windows程序迁移到Linux上 ...
- 如何托管ASP.NET Core应用到Windows Service中
(此文章同时发表在本人微信公众号"dotNET开发经验谈",欢迎右边二维码来关注.) 题记:正在构思一个中间件的设计,考虑是否既可以使用最新的技术,也可以兼顾传统的部署模式.所以有 ...
- 解决安装mysql的”A Windows service with the name MySQL already exists.“问题
如果以前安装过mysql,卸载重装,很可能会碰到"A Windows service with the name MySQL already exists."这样的提示.即服务已经 ...
- 使用Windows Service Wrapper快速创建一个Windows Service
前言 今天介绍一个小工具的使用.我们都知道Windows Service是一种特殊的应用程序,它的好处是可以一直在后台运行,相对来说,比较适合一些需要一直运行同时不需要过多用户干预的应用程序,这一类我 ...
- Windows Service--Write a Better Windows Service
原文地址: http://visualstudiomagazine.com/Articles/2005/10/01/Write-a-Better-Windows-Service.aspx?Page=1 ...
- 使用Python写Windows Service服务程序
1.背景 如果你想用Python开发Windows程序,并让其开机启动等,就必须写成windows的服务程序Windows Service,用Python来做这个事情必须要借助第三方模块pywin32 ...
- Install Jenkins Slave as Windows Service
https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins+as+a+Windows+service SC 直接创建windows s ...
- How to do code coverage test for windows service
First, instrument the exe or dll by command vsinstr -coverage the dll/exe second, start the performa ...
- windows Service 创建部署
Windows Service简介: 一个Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序.Windows服务程序虽然是可执行的,但是它不像一般的可执行文件通过双击就 ...
随机推荐
- C语言--第四次作业--数组
1.本章学习总结 1.1 思维导图 1.2本章学习体会及代码量学习体会 1.2.1学习体会 不知不觉都快学习C语言结束了,自从开始了数组的学习就感觉难度瞬间几何级上升鸭(让人头大,感觉到了各种绝望), ...
- OI退役
OI退役 by war 其实初中时就想自学编程,但是这个愿望并没有很强烈,加上中考的压力就没有实践.在中考前夕看到的现在的机房,哇,计算机奥赛.但是在机房里的可能是一神和奥神,也许有凯哥. 高一寒假学 ...
- python-关于字典与列表的一个作业
题目是这样的: 给定一个字典dict1,将其转换成dict2: dict1 = { 'fetal':[{u'key': u'sjzx_sy22_121.201.56.5', u'doc_count': ...
- linux系统,关于Python多版本共存
http://www.cnblogs.com/Yiutto/p/5962906.html 给个地址直接看八~
- DCDC设计指南二
DCDC电源设计指导:二 这一讲以一款SOP-8封装的Synchronous Step-Down Converter(同步降压转换器)电源IC为例,讲下电源的PCB设计. 如第一讲中所说,开始设计时就 ...
- Linux虚拟机搭建本地yum源
Yum本地源的配置 本教程是在虚拟机里安装Red Hat Enterprise Linux 7 ,以其为例使用iso文件进行Yum本地源的配置.所使用的软件如下: (1)虚拟机:Vmware work ...
- 命令行 app
[Getting Title at 38:53](http://click.pocoo.org/5/) 其实在 实际操作中 如果参数特别多的话,最好使用配置文件来操作,比如 yaml [YAML 语言 ...
- Python数据分析实战
Python数据分析实战(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1nlHM1IW8MYg3z79TUwIsWg 提取码:ux8t 复制这段内容后打开百度网盘手 ...
- hadoop fs -put 报错
[hadoop@master ~]$ ll total -rw-rw-r-- hadoop hadoop Apr : aaa drwxr-xr-x hadoop hadoop Jun Desktop ...
- android 第三次作业
android studio音乐播放器 一.实现功能: 1.读取本地SD中的所有音频文件 2.歌单列表展示,并显示音频具体信息 3.进度条显示当前播放进度,可滑动加速 4.点击歌单进行播放 5.实现暂 ...