1.首先加入引用:

using System.ServiceProcess;

2.控制启动服务:

public void Start()
{
var timeout = TimeSpan.FromSeconds(60);
try
{
_serviceController.Start();
_serviceController.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
catch (Exception ex)
{
throw new Exception("Service " + _serviceName + " is not started as expected, error: " + ex.Message);
}
}

3.控制停止服务:

public void Stop()
{
var timeout = TimeSpan.FromSeconds(60);
try
{
if (_serviceController.Status != ServiceControllerStatus.Stopped &&
_serviceController.Status != ServiceControllerStatus.StopPending)
{
_serviceController.Stop();
_serviceController.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
}
}
catch (Exception ex)
{
throw new Exception("Service " + _serviceName + " is not stoped as expected, error: " + ex.Message);
}
}

4.控制重启服务:

public void Restart()
{
Stop();
Start();
}

使用程序控制windows service启动/停止的更多相关文章

  1. windows Service启动带有管理员权限的GUI进程

    事情是这样的,公司的产品有个守护进程(windows Service)需要启动产品的主程序exe,让主程序它运行为管理员权限(因为主程序会加载一个插件,插件中有列出端口监听的功能,需要由端口查找到进程 ...

  2. MySQL的安装(比较详细的安装步骤,包括客户端和服务端的安装,还有环境变量的配置以及使用Windows service启动MySQL)

    1.MySQL官网下载操作系统对应的MySQL安装包,解压之后就可以直接使用(免安装). MySQL安装包,一种是MySQL Enterprise Edition (commercial)企业版,还有 ...

  3. Windows下启动停止Oracle11g服务-为解决系统变慢而生

    我们拿Oracle 11g作为例子. 首先在“开始=〉运行”中输入“services.msc”,按回车,进入“服务”控制台, 将 Oracle ORCL VSS Writer Service.Orac ...

  4. PostgreSQL windows service启动失败

    from: http://stackoverflow.com/questions/1251233/unable-to-run-postgresql-as-windows-servicepg_ctl - ...

  5. Aspnet Zero中使用Windows service (Topshelf)来承载Quartz.net任务

    Aspnet Zero使用Windows service (Topshelf)来承载Quartz.net任务 网上有很多关于如何使用Topshelf创建ABP的Quartz windows服务,但很少 ...

  6. 控制Linux下 mono 服务的启动停止

    当Window下的服务部署到Linux的时候,我们一般用Mono.service 来启动停止.参数比较多,不太好用.于是有个这个Shell脚本. 用法:moa s1 start #启动         ...

  7. Windows Service的转换与部署

    开发Windows Service,可能会碰到以下两种情况. 1. 直接开发一个Windows Service 网上有很多教程,可以参考这个: http://www.cnblogs.com/sorex ...

  8. 当Windows操作系统关机时,不会执行Windows Service的OnStop方法(转载)

    Windows Service OnStop when computer shutdown 问: I'm writing a Windows Service in C#. I want to take ...

  9. Windows Service 学习系列(二):C# windows服务:安装、卸载、启动和停止Windows Service几种方式

    一.通过InstallUtil.exe安装.卸载.启动.停止Windows Service 方法一 1.以管理员身份运行cmd 2.安装windows服务 切换cd C:\Windows\Micros ...

随机推荐

  1. 快速构建Windows 8风格应用4-FlipView数据控件

    原文:快速构建Windows 8风格应用4-FlipView数据控件 本篇博文主要介绍为什么使用FlipView控件.什么是FlipView控件.如何使用FlipView控件和FlipView控件最佳 ...

  2. C++中出现的计算机术语2

    C-style strings(C 风格字符串) C 程序把指向以空字符结束的字符数组的指针视为字符串.在 C++ 中,字符串字面值就是 C 风格字符串.C 标准库定义了一系列处理这样的字符串的库函数 ...

  3. Canvas入门(3):图像处理和渲染文本

    资源:http://www.ido321.com/997.html 一.图像处理(非特别说明,全部结果均来自最新版Google) 在HTML 5中,不仅能够使用Canvas API绘制图形,也能够用于 ...

  4. AngularJs ng-repeat

    AngularJs ng-repeat 必须注意的性能问题 AngularJs 的 ng-repeat 让我们非常方便的遍历数组生成 Dom 元素,但是使用不当也会有性能问题. 在项目中我们使用 ng ...

  5. git简单教材

    0)初始化 $ git config --global user.name "xxx" $ git config --global user.email "xxx@gma ...

  6. ps入门教程:画笔工具、铅笔工具、渐变工具等的使用

    本节课程主要内容:学习画笔工具.铅笔工具.颜色替换工具.历史记录画笔工具.历史记录艺术画笔工具.渐变工具和油漆桶 工具的应用.------------------------------------- ...

  7. java 权限 部分截图

    下载地址:http://download.csdn.net/detail/heyehuang/5857263

  8. Coding the Matrix Week 1 The Vector Space作业

    Coding the Matrix: Linear Algebra through Computer Science Applications 本周的作业较少,只有一个编程任务hw2.作业比较简单,如 ...

  9. TeamCity vs Jenkins: Which is the Better Continuous Integration (CI) Server for .NET Software Development?

    原文:http://www.excella.com/insights/teamcity-vs-jenkins-better-continuous-integration-server So, you’ ...

  10. 深入剖析Provider Model

    Membership三步曲之进阶篇 - 深入剖析Provider Model Membership 三步曲之进阶篇 - 深入剖析Provider Model 本文的目标是让每一个人都知道Provide ...