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数据库存储过程。
转载于:https://www.cnblogs.com/ZaraNet/p/9855299.html
C#使用Windows Service相关推荐
- 用VS(c#)创建、调试windows service以及部署卸载
同事问到windows service的东东,现在整理一下,用c#如何创建一个windows service,以及如何调试.部署.卸载. 一.创建windows service 1. 打开VS2008 ...
- 再谈Windows Service - 一个简单的自我例子和部署
我们的服务需要做两件自动的事情,一个是往表格中定时插入数据,另一个就是 往文件中定时插入内容 因此,我们需要做一个工作类,这个类可以是一个抽象类,如下: TaskWorker using System ...
- Windows Service
Windows Service程序写好后,通过服务让它跑起来 服务启动后,可以调试本地程序,通过附加到进程,会看到 这是我们刚刚创建的服务,右击启动,在vs附加进程中会看到 点击附加,打上断点,就可以 ...
- C#创建一个Windows Service
Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...
- 解决安装mysql的”A Windows service with the name MySQL already exists.“问题
解决安装mysql的"A Windows service with the name MySQL already exists."问题 参考文章: (1)解决安装mysql的&qu ...
- mysql安装提示create_MySQL5.1安装时出现Cannot create windows service for mysql.error:0
安装MySQL5.1过程中,我把以前MySQL5.0的GUI工具和服务器全部删掉,安装目录全部删掉,数据文件目录名字改掉,注册表用完美卸载清理了. 然后重启安装MySQL5.1(我以前遇到过服务启动不 ...
- Windows service无法删除怎么办?
自己用c#创建的windows service, 安装后,执行installutil /u 状态变成disable,却删除不了.删除的时候提示"The specified service h ...
- 什么是Windows Service应用程序?(转)
什么是Windows Service应用程序? Microsoft Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务 ...
- Windows Service:用C#创建Windows Service
现在的.NET框架已经为Windows service的开发提供足够强大的支持,你只需要关注service所要实现的逻辑,而完全不用关心service底层是如何实现的,相比以前用MFC来说,真是质的飞 ...
- 用 VC++ 2008 编写 Windows Service(系统服务)
用 VC++ 2008 编写 Windows Service(系统服务) 2008-03-30 08:08 现在许多 Windows Service 应用都可以用 c# 很好的完成,不过毕竟是托管代码 ...
最新文章
- 7月第4周全球域名商(国际域名)新增注册量TOP15
- Format函数用法
- Android 解决mac无法识别手机设备
- 腾讯GaiaStack容器平台负责人罗韩梅:All on GaiaStack
- 日语输入法电脑版_哪个日语输入法比较好用,日语输入法下载及使用教程
- JBuilder 2005 Enterprise注册破解
- Mysql函数和存储过程
- EmguCv模板匹配学习日记
- 2019年最新,免费检测僵尸粉软件,无打扰检测清理微信僵尸粉
- 华为交换机初始化_华为交换机初始设置
- 关于RJ45 网线接线问题
- 查看变量数据类型的python内置函数_查看变量类型的Python内置函数是
- 联想Lenovo手机平板安装谷歌服务框架Google, Play商店,安装套件GMS
- 手把手搭个vue的脚手架 - 2. 模板搭建
- 企业联合体的形式_企业联合体与垄断的初探
- 1、spss中做相关分析
- 2018 icpc 焦作站现场赛 E题题解
- Mac压缩文件怎么加密?BetterZip加密Word压缩文件教程
- 桌前检查、代码评审、走查
- Windows 下自定义某类型文件图标(例如.h5,.ipynb)