以编程方式编写服务

1.打开 Microsoft Visual Studio 2005 创建一个空项目;
2.添加对System、System.ServiceProcess、System.Configuration.Install的引用;
3.新建一个Windows服务文件Service1.cs,代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.ServiceProcess;
using System.Configuration.Install;
using System.ComponentModel;

namespace WindowsService2
{
    public class Service1 : ServiceBase
    {
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 应用程序的主入口点
        /// </summary>
        public static void Main()
        {
            ServiceBase.Run(new Service1());
        }

        /// <summary>
        /// 实例化对象
        /// </summary>
        public Service1()
        {
            this.ServiceName = "NewService";
            this.CanStop = true;
            this.CanPauseAndContinue = true;
            this.AutoLog = true;
        
        }

        /// <summary>
        /// 启动服务
        /// </summary>
        /// <param name="args"></param>
        protected override void OnStart(string[] args)
        {
            if (!File.Exists(@"C:\ServiceLog.log"))
            {
                File.Create(@"C:\ServiceLog.log");
            }

            File.AppendAllText(@"C:\ServiceLog.log", string.Format("启动时间:{0}\r\n", DateTime.Now.ToString()));

            //设置一个定时器
            System.Timers.Timer timer1 = new System.Timers.Timer();
            timer1.Interval = 3000;//间隔时间
            timer1.Elapsed += new System.Timers.ElapsedEventHandler(Done);//达到间隔时间执行的处理程序
            timer1.AutoReset = true;//每次间隔结束时引发 System.Timers.Timer.Elapsed 事件,如果仅在第一次引发设置为false
            timer1.Enabled = true;
        }

        /// <summary>
        /// 停止服务
        /// </summary>
        protected override void OnStop()
        {
            File.AppendAllText(@"C:\ServiceLog.log", string.Format("停止时间:{0}\r\n", DateTime.Now.ToString()));
        }

        /// <summary>
        /// 定时任务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Done(object sender, EventArgs e)
        {
            //这边可以做些其他操作

            File.AppendAllText(@"C:\ServiceLog.log", string.Format("执行定时任务:{0}\r\n", DateTime.Now.ToString()));
        }

        /// <summary>
        /// 释放
        /// </summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

//[RunInstaller(true)]
[RunInstallerAttribute(true)]
public class ProjectInstaller : Installer
{
    private System.ComponentModel.IContainer components = null;
    ServiceInstaller serviceInstaller1;
    ServiceProcessInstaller serviceProcessInstaller1;

    public ProjectInstaller()
    {
        this.serviceInstaller1 = new ServiceInstaller();
        this.serviceProcessInstaller1 = new ServiceProcessInstaller();

        this.serviceInstaller1.ServiceName = "NewService";
        this.serviceInstaller1.StartType = ServiceStartMode.Automatic;
        this.serviceProcessInstaller1.Account = ServiceAccount.LocalSystem;

        this.Installers.Add(serviceInstaller1);
        this.Installers.Add(serviceProcessInstaller1);
    }

    /// <summary>
    /// 释放
    /// </summary>
    /// <param name="disposing"></param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }
}


参考文章:

1.《如何:以编程方式编写服务》:http://msdn.microsoft.com/zh-cn/76477d2t(VS.80).aspx
2.ServiceProcessInstaller 类:http://msdn.microsoft.com/zh-cn/system.serviceprocess.serviceprocessinstaller(VS.80).aspx
3.ServiceInstaller 类:http://msdn.microsoft.com/zh-cn/system.serviceprocess.serviceinstaller(VS.80).aspx

上一篇: 如何:调试 Windows 服务应用程序
下一篇: XPath语法
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
最新日志:
评论: 0 | 引用: 0 | 查看次数: 3706
发表评论
登录后再发表评论!