写过Windows Service的朋友都知道服务是不可以直接在vs里面启动调试,我们必须修改Program.cs文件来达到我们调试的目的,等服务调试好了以后还要把代码改回来,显非常的不方便,在这里为大家介绍一种通用写法,这样就可以实现在vs里直接以控制台的方式调试服务程序并且在安装服务时候也不用修改代码可以直接安装。

下面看一个简单的Demo,相信看完这个大家都知道怎么写,本人觉得这种写法虽然很简单但挺巧妙的,所以写出来和大家分享一下!

一.服务类代码

    public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
} protected override void OnStart(string[] args)
{
Start();
} protected override void OnStop()
{
Stop();
} //自定义两个方法
public void Start()
{
//启动服务...
} public void Stop()
{
//关闭服务..
}
}

二.Program.cs代码

    static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
static void Main(string[] args)
{ var consoleMode = args.Any(arg => !string.IsNullOrEmpty(arg) && arg.ToLower() == "/console"); if (consoleMode)
{
Console.WriteLine("以控制台方式启动服务"); Service1 service1 = new Service1();
service1.Start(); Console.ReadLine();
service1.Stop();
return;
}
       
       //以Windows Service方式启动服务
            ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
}

补充:1.修改应用程序输出类型改为控制台应用程序

2.给debug添加参数

关于Windows Service的一个编写技巧的更多相关文章

  1. 使用Windows service创建一个简单的定时器

    一.需求 我们有时候可能会想要做一些定时任务,例如每隔一段时间去访问某个网站,或者下载一些东西到我们服务器上等等之类的事情,这时候windows service 是一个不错的选择. 二.实现 1.打开 ...

  2. C# 编写Windows Service(windows服务程序)

    C# 编写Windows Service(windows服务程序)   Windows Service简介: 一个Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序.W ...

  3. C# 编写Windows Service(windows服务程序)【转载】

    [转]http://www.cnblogs.com/bluestorm/p/3510398.html Windows Service简介: 一个Windows服务程序是在Windows操作系统下能完成 ...

  4. C#Windows Service程序的创建安装与卸载

    C#Windows Service程序的创建安装与卸载 一.开发环境 操作系统:Windows7x64 sp1 专业版 开发环境:Visual studio 2013 编程语言:C# .NET版本: ...

  5. windows Service 创建部署

    Windows Service简介: 一个Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序.Windows服务程序虽然是可执行的,但是它不像一般的可执行文件通过双击就 ...

  6. 创建Windows Service

    基本参照使用C#创建Windows服务,添加了部分内容 目录 创建Windows Service 可视化管理Windows Service 调试 示例代码 创建Windows Service 选择C# ...

  7. go & Windows Service

    相关库 https://godoc.org/golang.org/x/sys/windows/svc https://github.com/kardianos/service https://gith ...

  8. 使用Windows Service Wrapper快速创建一个Windows Service

    前言 今天介绍一个小工具的使用.我们都知道Windows Service是一种特殊的应用程序,它的好处是可以一直在后台运行,相对来说,比较适合一些需要一直运行同时不需要过多用户干预的应用程序,这一类我 ...

  9. 使用Windows Service Wrapper快速创建一个Windows Service 如nginx

    前言 今天介绍一个小工具的使用.我们都知道Windows Service是一种特殊的应用程序,它的好处是可以一直在后台运行,相对来说,比较适合一些需要一直运行同时不需要过多用户干预的应用程序,这一类我 ...

随机推荐

  1. php插入代码数据库

    插入代码:<?php $con = mysql_connect("localhost","peter","abc123"); if ( ...

  2. C++ 匿名对象产生场景

    //匿名对象产生的三种场景 #include<iostream> using namespace std; class Point{ public: Point(int a,int b){ ...

  3. 【转】【Linux】grep命令详解

    简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ...

  4. UP与瀑布模型

    软件开发过程(Software development process)描述了构造.部署以及维护软件的方式.(就是先做什么后做什么)统一过程(The Unified Software Developm ...

  5. html -- contenteditable 属性:指定元素内容是否可编辑

    所有主流浏览器都支持 contenteditable 属性 定义和用法 contenteditable 属性指定元素内容是否可编辑. 注意: 当元素中没有设置 contenteditable 属性时, ...

  6. java中不用BigInteger实现超大整数的乘法操作

    昨天看到一个题目:计算1234!,不能用BigInteger类 众所周知阶乘的数据会非常大,经常使用的int和long型根本不够用.一般想到的仅仅有BigInteger类,可是题目中明白说了不能用,所 ...

  7. Sublime 插件补充

    开启vim模式+autosave+Livereload插件 安装参考:emmmet http://www.cnblogs.com/wuheng1991/p/6144955.html

  8. java 问题

    1. 在ezmorph包中 有个 引用类时 写法为import [Z; 为什么加个[看不懂

  9. [转]Shell脚本中发送html邮件的方法

    <span "="">作为运维人员,免不了要编写一些监控脚本,并将监控结果及时的发送出来.那么通过邮件发送是比较常用的一种通知方式了.通常的,如果需要发送的内 ...

  10. configChanges

    android中的组件Activity在manifest.xml文件中可以指定参数android:ConfigChanges,用于捕获手机状态的改变. 在Activity中添加了android:con ...