在.Net中用C#创建Windows Service,其实很简单,按照以下的步骤就可以做出一个简单的Windows Service。

1.首先在创建工程的时候选择Windows Service,这样.Net会自动生成Windows Service的框架;

2.完成Windows Service的相应事件,主要是OnStart和OnStop这两个事件,完成后大致代码如下:

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Diagnostics;

using System.ServiceProcess;

using System.IO;

using System.Threading;

namespace WinSDemo

{

public class WinSDemo : System.ServiceProcess.ServiceBase

{

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

private bool blnStopThread;

private Thread thdMain;

public WinSDemo()

{

// This call is required by the Windows.Forms Component Designer.

InitializeComponent();

// TODO: Add any initialization after the InitComponent call

}

// The main entry point for the process

static void Main ()

{

System.ServiceProcess.ServiceBase[] ServicesToRun;

// More than one user Service may run within the same process. To add

// another service to this process, change the following line to

// create a second service object. For example,

//

ServicesToRun = new System.ServiceProcess.ServiceBase[] { new WinSDemo() };

System.ServiceProcess.ServiceBase.Run(ServicesToRun);

}

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

//

// WinSDemo

//

this.CanPauseAndContinue = true;

this.ServiceName = "MyTest";

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

/// <summary>

/// Set things in motion so your service can do its work.

/// </summary>

protected override void OnStart(string[] args)

{

// TODO: Add code here to start your service.

thdMain=new Thread(new ThreadStart(WriteLog));

thdMain.Start();

}

/// <summary>

/// Stop this service.

/// </summary>

protected override void OnStop()

{

// TODO: Add code here to perform any tear-down necessary to stop your service.

blnStopThread=true;

thdMain.Join();

}

protected override void OnPause()

{

thdMain.Suspend();

}

protected override void OnContinue()

{

thdMain.Resume();

}

protected void WriteLog()

{

StreamWriter myWriter=null;

do

{

Process.Start("net","send localhost yourValue");

try

{

myWriter=new StreamWriter("c://MyLog.txt",true);

myWriter.WriteLine(DateTime.Now.ToString());

myWriter.Close();

}

catch{};

Thread.Sleep(5000);

}while(blnStopThread==false);

}

}

}

注:为了使自己能更好的识别自己写的Windows Service,建议在InitializeComponent修改Service的名称。

3.为了使自己写的Service能加载到系统中去,光靠以上步骤是不够;接下来,向当前的工程添加Service Installer,在其中设置Service安装后的起始状态,代码如下:

using System;

using System.Collections;

using System.ComponentModel;

using System.Configuration.Install;

using System.ServiceProcess;

namespace WinSDemo

{

/// <summary>

/// Summary description for WinSDemoIns.

/// </summary>

[RunInstaller(true)]

public class WinSDemoIns : System.Configuration.Install.Installer

{

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

private ServiceInstaller serviceInstaller;

private ServiceProcessInstaller processInstaller;

public WinSDemoIns()

{

// This call is required by the Designer.

InitializeComponent();

// TODO: Add any initialization after the InitComponent call

processInstaller = new ServiceProcessInstaller();

serviceInstaller = new ServiceInstaller();

// Service will run under system account

processInstaller.Account = ServiceAccount.LocalSystem;

// Service will have Start Type of Manual

serviceInstaller.StartType = ServiceStartMode.Automatic;

serviceInstaller.ServiceName = "MyTest";

Installers.Add(serviceInstaller);

Installers.Add(processInstaller);

}

#region Component Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

components = new System.ComponentModel.Container();

}

#endregion

}

}

4.完成以上的步骤,代码的部分就完成了,编译成可执行文件,再用.Net的Service安装工具就行了,即在Dos窗口中,键入“installutil yourService.exe”,这样执行就可以了,相反,如果想卸载Service的话,加一个参数就可以了,即“installutil /u yourService.exe”。注意有可能.Net的路径在环境变量中不存在,可能直接执行是不能成功的,希望先找到“installutil.exe”存在的目录,大致在“/WINDOWS/Microsoft.NET/Framework/v1.1.4322”目录下。

至于以后Service的部署,由于.Net写的程序,运行环境必须要安装.Net Framework,所以在其他机器安装自己写的Service时候,一定要先安装.Net运行环境。

如何用C#创建Windows Service相关推荐

  1. 【windows service】C# 创建Windows Service(Windows服务)程序

    C# 创建Windows Service(Windows服务)程序 很多年前大概08年做了一个windows桌面程序推送邮件给用户的程序,然后通过windows 计划任务每隔N分钟重复执行. 今天闲暇 ...

  2. C# 创建Windows Service(Windows服务)程序

    工具: Visual Studio Community 2015 写在前面: Windows可以在后台做一些任务,今天分享下Windows Service创建.安装.调试的方法以及一些异常问题的处理 ...

  3. 创建Windows Service

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

  4. Windows Service:用C#创建Windows Service

    现在的.NET框架已经为Windows service的开发提供足够强大的支持,你只需要关注service所要实现的逻辑,而完全不用关心service底层是如何实现的,相比以前用MFC来说,真是质的飞 ...

  5. 【C#】C#创建Windows Service服务

    目录结构: contents structure [+] 创建Windows服务 配置 安装Windows服务 在Visual Studio中调试 常见问题 最近写了一个TCP连接的程序,由于这种通信 ...

  6. C#创建一个Windows Service

    Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...

  7. 用VS(c#)创建、调试windows service以及部署卸载

    同事问到windows service的东东,现在整理一下,用c#如何创建一个windows service,以及如何调试.部署.卸载. 一.创建windows service 1. 打开VS2008 ...

  8. C#创建和部署Windows Service程序

      接下来这里我对Windows Service程序的创建.部署等方面做简单描述. 前言   这里首先有个问题,什么是Windows Service,也就是我们Windows服务.微软的官方定义是这样 ...

  9. SAP创建Web Service以及用ABAP调用

    如果运行 soamanager 打不开,网页显示 devaix .zhongpin.com ,打开 C :\WINDOWS\system32\drivers\etc, 用记事本打开 hosts 文件 ...

最新文章

  1. udacity 同学 pca 客户细分实例操作
  2. linux命令历史详解
  3. 注册CSDN帐号的见闻续
  4. Java虚拟机详解04----GC算法和种类【重要】
  5. 前松鼠拼拼产品总监吴天:做一个接地气的产品经理
  6. 05_NoSQL数据库之Redis数据库:Redis的常用命令,键值相关命令和服务器相关命令
  7. Android开发之Intent.Action
  8. 解决redis启动时的警告
  9. void Update ( ) 更新 void FixedUpdate ( )
  10. 陕西省高级职称 计算机要求,陕西省卫生高级职称评审申报条件
  11. 计组之中央处理器:8、五段式指令流水线
  12. 计算机跨考英语口译,英语口译学习网_考研复试经验:跨考过来人谈复试感受_沪江英语...
  13. (转)C++中extern “C”含义深层探索
  14. B站DR-CANup主电路系统建模_基尔霍夫定律的解题过程分析
  15. WebSocket请求过程分析及实现Web聊天室
  16. Json.NET特殊处理64位长整型数据
  17. m3u8 video ios h5_移动端H5页面踩坑记
  18. Spring定时器 cron详解
  19. 等保2.0三级移动互联安全扩展要求
  20. 股民误将向日葵当成农业股

热门文章

  1. LG OnScreen Control 软件提示‘镜像显示器‘ BUG
  2. linux+nas+私有云软件,自建家庭私有云NAS——磁盘管理系统
  3. 从粗放到精细,能量采集技术如何赋能农业智慧升级?
  4. 优化算法中的鞍点与梯度下降
  5. 极客时间-左耳听风-程序员攻略-机器学习和人工智能
  6. DXT 图片压缩(DXTC/DirectX Texture Compression Overview)
  7. VVC代码阅读 xCheckModeSplit()函数 (中间CABAC还没看)最后代码还没看完
  8. 「UbuntuROS」Ubuntu系统下几个必不可少的设置和软件|1-4
  9. 机器人庄园作文_赛尔号想象作文
  10. linux系统有哪些版本 linux系统哪个版本好用