码迷,mamicode.com
首页 > Windows程序 > 详细

第一个WindowService服务

时间:2017-07-30 22:06:08      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:class   eve   div   proc   rgs   ini   ide   需要   void   

背景:Web项目中需要定时执行一段程序

方法: 1.新建一个WindowService项目

    2.添加代码

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 = 10000;
            timer.Start();
        }
        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (!Debugger.IsAttached)
            {
                Debugger.Launch();
            }
            Console.WriteLine("Time elapsed");
        }

        protected override void OnStop()
        {
            if (timer != null)
            {
                timer.Stop();
            }
        }
    }

 

    3.添加安装程序

     技术分享

        3.1修改serviceProcessInstaller1属性中的account

      技术分享

技术分享

            3.2 修改serviceInstaller1属性中的ServiceName(此名称为服务的名称)

              技术分享    4.添加外部工具

        技术分享

 

  技术分享

    5.运行

技术分享

    技术分享

可以看到成功了

技术分享

    6.删除服务 cmd.exe  

    sc delete "ServiceName"

第一个WindowService服务

标签:class   eve   div   proc   rgs   ini   ide   需要   void   

原文地址:http://www.cnblogs.com/engineerlm/p/7260471.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!