有很多时候,我们需要创建Windows Service。 这篇文章可以算是一个入门指南吧,希望对初学者有帮助.

要创建Windows Service, 首先选择Windows服务项目,如下图:

这里我想创建一个Windows服务,定时的执行一些任务。

public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
} protected override void OnStart(string[] args)
{
} protected override void OnStop()
{
}
}

OnStart :服务启动的时候执行,

OnStop:服务停止的时候执行

为了定时的执行任务,我修改代码如下:

namespace TimeWindowsService
{
public partial class Service1 : ServiceBase
{
System.Timers.Timer timer = null; public Service1()
{
InitializeComponent();
} protected override void OnStart(string[] args)
{
timer = new System.Timers.Timer();
timer.Elapsed += timer_Elapsed;
timer.Interval = 1000;
timer.Start();
} void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine("Time elapsed");
} protected override void OnStop()
{
if (timer != null)
{
timer.Stop();
}
}
}
}

按F5运行代码,会提示:

这代表我们必须先使用Installutil.exe 来安装服务。

当然你可以打开命令行,然后输入命令来安装服务,不过在这里我打算使用外部工具来执行InstallUtil.exe。

其中命令是:C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe

点击确定,可以发现一个InstallService 命令。

点击InstallService,可以发现屏幕闪了一下,然后又过了,很明显是因为我们的命令出错了。

注意,我们在添加InstallService的时候,选择的是,所以即使有错误信息,也会一闪而过。

修改InstallService的命令如下:

再次运行InstallService,可以看到:

这代表InstallService 的时候出错了,找不到文件,我们希望它找的是TimeWindowsService.exe,

而不是\bin\debug\TimeWindowsService

所以正确的设置InstallService命令应该是下面这样:

保存,然后运行,结果如下:

还是没有安装成功,因为没有安装程序。

有很多教程会告诉你,需要添加一个像下面这样的类:http://www.cnblogs.com/jjstar/articles/20353.aspx

using System.Collections;
using System.Configuration.Install;
using System.ServiceProcess;
using System.ComponentModel; namespace WindowsService1
{
/// <summary>
/// myInstall 的摘要说明。
/// </summary>
///
[RunInstaller(true)]
public class myInstall : Installer
{ private ServiceInstaller serviceInstaller;
private ServiceProcessInstaller processInstaller;
public myInstall()
{
processInstaller = new ServiceProcessInstaller();
serviceInstaller = new ServiceInstaller(); processInstaller.Account = ServiceAccount.LocalSystem;
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.ServiceName = "WindowsService1"; Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
}
}
}

对于记性不好的人来说,很容易问的一个问题是:VS有提供相关的快捷键吗?,当然,VS提供了。

在Service1.cs 设计页面点击,添加安装程序,vs 就会自动帮你生成一个Installer 的类。

添加的类是:

serviceProcessInstaller1 :

安装一个可执行文件,该文件包含扩展 System.ServiceProcess.ServiceBase 的类。 该类由安装实用工具(如 InstallUtil.exe)在安装服务应用程序时调用。

serviceInstall1:

安装一个类,该类扩展 System.ServiceProcess.ServiceBase 来实现服务。 在安装服务应用程序时由安装实用工具调用该类。

细心的读者看看他们的属性就知道区别在哪里了。

为了简单,将account 设置为LocalSystem.

好了,安装程序已经弄完了,接着上面的InstallService 吧,注意要编译程序啊。

可以看到已经成功的安装服务了。

从任务管理器中,你也可以看到安装的服务:

OK,接着按F5 调试吧,可是:

依然出现了这个提示框,很显然,我们不能按F5 来调试windows服务

有很多文章讲述了如何调试windows服务。

1:比如这篇文章:http://www.cnblogs.com/downmoon/archive/2009/09/16/1567643.html,所采用的AttachProcess 方法。

可是有时候,由于各种各样的原因,服务无法启动,或者是服务由于某些原因而自动停止,所以也就不太容易附加到进程去调试了。

2:还有一些修改代码的方法,比如:

添加一个DebugOnStart 的方法

public void DebugOnStart()
{
this.OnStart(null);
} protected override void OnStart(string[] args)
{
timer = new System.Timers.Timer();
timer.Elapsed += timer_Elapsed;
timer.Interval = 1000;
timer.Start();
}

然后修改main函数如下:

static void Main()
{
//ServiceBase[] ServicesToRun;
//ServicesToRun = new ServiceBase[]
//{
// new Service1()
//};
//ServiceBase.Run(ServicesToRun); new Service1().DebugOnStart();
}

效果如下:

3:在构造函数中用Thread.Sleep. 然后快速的附加到进程。

这样,服务就会需要10秒才能启动,有10秒的时间,肯定可以”附加到进程”的吧

我个人比较喜欢的一种方式:

void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (!Debugger.IsAttached)
{
Debugger.Launch();
} Console.WriteLine("Time elapsed");
}

这样在启动Service的时候,会提示:

点击确定后:

这种方式是,你可以在任何时间,任何需要调试的地方添加上面的语句。

具体的可以参考Debugger 类。

参考的文章:http://www.codeproject.com/Articles/19914/How-to-debug-a-Windows-Service-and-not-die-in-the

Windows 服务入门指南的更多相关文章

  1. C#创建Windows服务入门图解(VS2010)

    C#创建Windows服务入门图解(VS2010) Windows服务大家都知道,比如Audio.Theme都是大家比较熟悉的服务,他们可以设为自动启动的,并且在注册表的开机自启动项里是没有痕迹的.所 ...

  2. .Net创建windows服务入门

    本文主要记录学习.net 如何创建windows服务. 1.创建一个Windows服务程序 2.新建安装程序 3.修改service文件 代码如下 protected override void On ...

  3. C#开发Windows服务 入门

    Microsoft Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序. 服务可以在计算机启动时自动启动,可以暂停和重新启动而且 ...

  4. c#创建windows服务入门教程实例

    用c#中创建一个windows服务非常简单,与windows服务相关的类都在System.ServiceProcess命名空间下. 每个服务都需要继承自ServiceBase类,并重写相应的启动.暂停 ...

  5. 《Office 365开发入门指南》上市说明和读者服务

    写在最开始的话 拙作<Office 365开发入门指南>上周开始已经正式在各大书店.在线商城上市,欢迎对Office 365的开发.生态感兴趣的开发者.项目经理.产品经理参考本书,全面了解 ...

  6. C# Windows服务开发从入门到精通

    一.课程介绍 大家都知道如果想要程序一直运行在windows服务器上,最好是把程序写成windows服务程序:这样程序会随着系统的自动启动而启动,自动关闭而关闭,不需要用户直接登录,直接开机就可以启动 ...

  7. 分布式服务框架 Zookeeper(三)官方入门指南

    入门指南:使用ZooKeeper来协调分布式应用 这篇文档包含了让你快速上手ZooKeeper的信息.主要是针对那些想要试一把ZooKeeper的开发人员,包含了安装一个单一ZooKeeper服务器的 ...

  8. Microsoft Orleans 之 入门指南

    Microsoft Orleans 在.net用简单方法构建高并发.分布式的大型应用程序框架. 原文:http://dotnet.github.io/orleans/ 在线文档:http://dotn ...

  9. 《Gulp 入门指南》 : 使用 gulp 压缩 JS

    <Gulp 入门指南> : 使用 gulp 压缩 JS 请务必理解如下章节后阅读此章节: 安装 Node 和 gulp 访问论坛获取帮助 压缩 js 代码可降低 js 文件大小,提高页面打 ...

随机推荐

  1. vs2013 RTM 激活码

    BWG7X-J98B3-W34RT-33B3R-JVYW9

  2. jQuery EasyUI---validatebox 校验规则扩展

    EasyUI 的 validatebox 插件, 验证规则相对比较单一也比较少,如下. rules: { email:{ validator: function(value){ return ...? ...

  3. addChildViewController相关api深入剖析

    注:本文根据个人的实践和理解写成,若有不当之处欢迎斧正和探讨! addChildViewController是一个从iOS5开始支持的api接口,相关的一系列的接口是用来处理viewcontrolle ...

  4. .Net程序员安卓学习之路5:使用xutils注入View和事件以及图片的显示

    xUtils注入和图片显示 一.xUtils注入 引用官方介绍: ViewUtils模块: •android中的ioc框架,完全注解方式就可以进行UI,资源和事件绑定: •新的事件绑定方式,使用混淆工 ...

  5. storyboard中xib文件不加载问题

    今天在用Xcode6自定义视图控制器时附带了一个XIB文件,然后把自定义的类绑定到storyboard的ViewController,如图所示  , 发现RootViewController对应的xi ...

  6. Magento Error – The directory is not writable by server.

    When trying to use the insert image functionality in Magento if you receive an error saying: “The di ...

  7. imx6 KEY_ROW4 power output high fail

    imx6 KEY_ROW4的pin设置成gpio之后,不能够输出高电平.解决方法记录于此. 参考链接: https://lists.yoctoproject.org/pipermail/meta-fr ...

  8. 微信JS SDK Demo

    微信JS-SDK 分享到朋友圈 分享给朋友 分享到QQ 拍照或从手机相册中选图 识别音频并返回识别结果 使用微信内置地图查看位置原文:http://www.cnblogs.com/txw1958/p/ ...

  9. spring消费RESTfull服务

    使用RestTemplate做这件事非常简单 package hello; import org.slf4j.Logger; import org.slf4j.LoggerFactory; impor ...

  10. C++ 基础复习 1

    1. 友元 友元的作用是,友元函数内部可以直接访问外围类的private的字段或方法.通俗的理解就是解决了访问权限的问题. 1) 有点像java的内部类,但是只能在外围类中声明,定义(实现)部分要写在 ...