1.Windows服务简单介绍

  Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序,主要用于长时间运行的功能或者执行定时任务。一般情况下,用户不能通过用户界面来安装和启动Windows服务程序,服务的启动方式有自动启动和手动启动两种。我们可以在运行中输入services.msc 命令打开服务管理界面,在这里可以进行启动或者停止服务:

2.C#开发windows服务

1.windows服务开发流程

1.1 windows的基本开发流程

  使用.Net可以很方便地开发windows服务,下边通过一个栗子演示下开发windows服务的简单流程。

  首先创建一个Windows服务项目,如下图我们创建了一个叫MyService的服务项目

  然后打开自动生成的Service1.cs,该文件默认有两个方法: OnStart()和OnStop() ,OnStart()指定当服务启动时要执行的操作,OnStop()指定当服务停止时要执行的操作。 我们重写OnStart()方法,让服务开启的时候,每隔一秒在一个文本文件中记录当前时间,代码如下图

然后在右键Service1.cs文件,选择查看设计器,在界面上右键选择添加安装程序,如下图

修改serviceInstallerserviceProcessInstaller,如下图所示,到这里服务的创建工作就完成了

1.2 安装和卸载Windows服务项目

   我们先通过下边命令来安装和卸载服务,其中第二行命令用于安装服务,命令的地址是我们服务项目生成的exe文件所在路径,第三行命令用于卸载服务。

cd C:\Windows\Microsoft.NET\Framework\v4.0.30319

InstallUtil C:\Users\ZDZN\source\repos\WindowsServiceDemo\WindowsServiceInstall\bin\Debug\MyService.exe    ----------安装服务
InstallUtil -u C:\Users\ZDZN\source\repos\WindowsServiceDemo\WindowsServiceInstall\bin\Debug\MyService.exe ----------卸载服务

执行成功后在服务管理界面可以看到我们刚才安装的服务,这说明服务已经安装成功了:

我们可以在命令行中通过以下命令控制服务的开启和停止:

net start MyService //开启服务
net stop MyService //停止服务
sc delete MyService //删除服务

如果程序没有错误的话,服务启动后会在F盘中生成一个hello.txt文件,内容如下:

2.通过Winform控制windows服务

  虽然通过命令行可以实现Windows服务的安装、卸载、启动、停止等操作,但是这样对于用户来说还是太过麻烦。如果能通过Winform来实现这些功能,用户使用起来就方便多了。怎么让Winform控制windows服务呢?

首先要知道的一些知识:使用AssemblyInstaller可以进行Windows服务的安装和卸载,ServiceController可以控制windows服务的启动,停止,暂停,继续等,知道了这些就容易实现了。

添加一个winform应用程序,引用我们的服务项目,程序代码如下,这里的命名都很明显就不详细说明:

    public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//设置服务地址和服务名
string serviceFilePath = $"{Application.StartupPath}/MyService.exe";
string serviceName = "MyService"; private void btnInstall_Click(object sender, EventArgs e)
{
Task.Run(() =>
{
ShowMessage("开始安装服务...");
if (IsServiceExisted(serviceName))
{
UninstallService(serviceFilePath);
}
InstallService(serviceFilePath);
});
} private void btnStart_Click(object sender, EventArgs e)
{
Task.Run(() =>
{
ShowMessage("服务正在启动...");
if (IsServiceExisted(serviceName))
{
ServiceStart(serviceName);
ShowMessage("服务已启动");
} });
} private void btnStop_Click(object sender, EventArgs e)
{
Task.Run(() =>
{
ShowMessage("服务正在停止...");
if (IsServiceExisted(serviceName))
{
ServiceStop(serviceName);
ShowMessage("服务已停止");
}
});
} private void btnUninstall_Click(object sender, EventArgs e)
{
Task.Run(() =>
{
ShowMessage("开始卸载服务...");
if (this.IsServiceExisted(serviceName))
{
this.ServiceStop(serviceName);
}
this.UninstallService(serviceFilePath);
});
} #region 方法封装
private void ShowMessage(string s)
{
Action setState = () =>
{
myTxtBox.AppendText(DateTime.Now.ToString("f") + ":");
myTxtBox.AppendText(Environment.NewLine);
myTxtBox.AppendText(s);
myTxtBox.AppendText(Environment.NewLine); };
myTxtBox.Invoke(setState);
} //判断服务是否存在
private bool IsServiceExisted(string serviceName)
{
try
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController sc in services)
{
if (sc.ServiceName.ToLower() == serviceName.ToLower())
{
return true;
}
}
return false;
}
catch (Exception ex)
{
ShowMessage(ex.Message);
return false;
}
} //安装服务
private void InstallService(string serviceFilePath)
{
try
{
using (AssemblyInstaller installer = new AssemblyInstaller())
{
installer.UseNewContext = true;
installer.Path = serviceFilePath;
IDictionary savedState = new Hashtable();
installer.Install(savedState);
installer.Commit(savedState);
ShowMessage("服务安装完成");
}
}
catch (Exception ex)
{
ShowMessage(ex.Message);
}
} //卸载服务
private void UninstallService(string serviceFilePath)
{
try
{
using (AssemblyInstaller installer = new AssemblyInstaller())
{
installer.UseNewContext = true;
installer.Path = serviceFilePath;
installer.Uninstall(null);
ShowMessage("服务卸载完成");
}
}
catch (Exception ex)
{
ShowMessage(ex.Message);
}
} //启动服务
private void ServiceStart(string serviceName)
{
try
{
using (ServiceController control = new ServiceController(serviceName))
{
if (control.Status == ServiceControllerStatus.Stopped)
{
control.Start();
}
}
}
catch (Exception ex)
{
ShowMessage(ex.Message);
}
} //停止服务
private void ServiceStop(string serviceName)
{
try
{
using (ServiceController control = new ServiceController(serviceName))
{
if (control.Status == ServiceControllerStatus.Running)
{
control.Stop();
}
}
}
catch (Exception ex)
{
ShowMessage(ex.Message);
}
}
#endregion #region 页面元素 private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false; timer1.Interval = ;
timer1.Enabled = true;
} //btn状态
private void ButtonState()
{
if (IsServiceExisted(serviceName))
{
btnInstall.Enabled = false;
btnUninstall.Enabled = true;
using (var service = new ServiceController(serviceName))
{
if (service.Status == ServiceControllerStatus.Running || service.Status == ServiceControllerStatus.StartPending)
{
btnUninstall.Enabled = false;
btnStart.Enabled = false;
btnStop.Enabled = true;
}
else
{
btnUninstall.Enabled = true;
btnStart.Enabled = true;
btnStop.Enabled = false;
}
}
}
else
{
btnInstall.Enabled = true;
btnUninstall.Enabled = false;
btnStart.Enabled = false;
btnStop.Enabled = false;
}
}
//底部label状态
private void LabelState()
{
if (!IsServiceExisted(serviceName))
{
this.labState.Text = $"【{serviceName}】服务未安装";
return;
}
using (var service = new ServiceController(serviceName))
{
switch (service.Status)
{
case ServiceControllerStatus.Running:
this.labState.Text = $"【{serviceName}】服务已启动";
break;
case ServiceControllerStatus.StartPending:
this.labState.Text = $"【{serviceName}】服务正在启动...";
break;
case ServiceControllerStatus.Stopped:
this.labState.Text = $"【{serviceName}】服务已停止";
break;
case ServiceControllerStatus.StopPending:
this.labState.Text = $"【{serviceName}】服务正在停止...";
break;
default:
break;
}
}
} private void timer1_Tick(object sender, EventArgs e)
{
ButtonState();
LabelState();
}
#endregion
}

完成这一步后,我们就可以通过可视化界面对服务的安装,卸载,运行,停止进行控制了。

补充:使用TopShelf也可以实现Windows服务的快速开发,参考文档 http://docs.topshelf-project.com/en/latest/index.html

C#开发Windows服务详细流程的更多相关文章

  1. C#开发Windows服务 附简单实例实现禁止QQ运行

    本实例主要实现下面三个基本功能 1.C#开发windows服务 2.禁止QQ等程序运行 3.为windows服务创建自动安装程序 下面针对这三个基本功能进行实现 一.C#开发windows服务 Win ...

  2. C# DateTime的11种构造函数 [Abp 源码分析]十五、自动审计记录 .Net 登陆的时候添加验证码 使用Topshelf开发Windows服务、记录日志 日常杂记——C#验证码 c#_生成图片式验证码 C# 利用SharpZipLib生成压缩包 Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库

    C# DateTime的11种构造函数   别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Glob ...

  3. C#开发windows服务如何调试——资料整理

    原文标题:C# Windows服务程序如何进行调试 原文地址:https://jingyan.baidu.com/article/456c463b18e1b00a583144b3.html 第一种: ...

  4. 使用Visual Studio 2015 Community 开发windows服务

    昨天研究在.NET下开发Windows服务程序,期间遇到一些小问题,这里仅将自己的开发过程和需要注意的地方写下和广大网友分享……  1.基础   Windows服务是指系统启动时能够自己运行的程序.W ...

  5. VS2013开发Windows服务项目

    这篇随笔里,我将介绍如何用VS2013开发Windows服务项目,实现的功能是定时发送电子邮件. 开发环境:VS2013,SQL Server2008,采用C#语言开发 步骤一:创建Windows服务 ...

  6. 使用Topshelf开发Windows服务、log4net记录日志

    开发windows服务,除了在vs里新建服务项目外(之前有写过具体开发方法,可点击查看),还可以使用Topshelf. 不过使用topshelf需要.netframework 4.5.2版本,在vs2 ...

  7. 开发Windows服务

          在开发Windows服务时需要注意一点,如果在开发完成后,需要通过命令来进行安装的,那么在开发的时候,需要在服务类上面添加一个安装文件.如下图:               添加完成后,就 ...

  8. c#金额转换成中文大写金额 .Net开发Windows服务

    c#金额转换成中文大写金额   2018-08-24 转别人 c#金额转换成中文大写金额 /// <summary> /// 金额转换成中文大写金额 /// </summary> ...

  9. C#常规开发Windows服务

    .Net平台下开发Windows服务的支持库很多,除了通过标准的Windows服务项目,还有一些优秀的开源架构比如:TopSelf:本文以常规项目为例 一.开发 1.新建[Windows服务] 项目: ...

随机推荐

  1. 【转载】DSP基础--定点小数运算

    在FPGA实现算法过程中,大多数情况是用占用资源较少,延迟较低的定点数代替浮点数参与运算.那么浮点与定点数之间的区别以及转换方式是怎么的?下边这篇博文详细说明了这一问题.虽然是针对DSP芯片的,但思想 ...

  2. C#基础知识之反射

    http://blog.csdn.net/educast/article/details/2894892 两个现实中的例子:1.B超:大家体检的时候大概都做过B超吧,B超可以透过肚皮探测到你内脏的生理 ...

  3. 解决IntelliJ IDEA 创建Maven项目速度慢问题

    IntelliJ IDEA 创建maven项目速度很慢,甚至卡住不动了. 原因 IDEA根据maven archetype的本质,其实是执行mvn archetype:generate命令,该命令执行 ...

  4. java 常用工具整理

    mapUtil map操作工具类 <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 ...

  5. [LeetCode] 16. 最接近的三数之和

    题目链接:https://leetcode-cn.com/problems/3sum-closest/ 题目描述: 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 num ...

  6. 初学Kafka工作原理流程介绍

    Apache kafka 工作原理介绍 消息队列技术是分布式应用间交换信息的一种技术.消息队列可驻留在内存或磁盘上, 队列存储消息直到它们被应用程序读走.通过消息队列,应用程序可独立地执行--它们不需 ...

  7. 学号 20175329 2018-2019-3《Java程序设计》第九周学习总结

    学号 20175329 2018-2019-3<Java程序设计>第八周学习总结 教材学习内容总结 第十五章 泛型 可以使用"class 名称"声明一个类,为了和普通的 ...

  8. <转>性能测试指标

    下午在家看书,清理收藏栏的内容,翻出来几篇去年收藏的博文,此时再看,真切的感觉到了自己这一年的成长,分享出来,希望看到的童鞋都能有所得,就好... 原文地址:性能测试指标 一.通用指标 指Web应用服 ...

  9. 292. Nim Game(easy)

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  10. 基于mybatis基本操作

    实体 在对上述三个实体操作中了解mybatis的实现 全部代码 pro1是另外的一个完整项目用spring和struts2 hibernate 下载  https://github.com/Danov ...