首页 > Windows开发 > 详细

使用TopShelf做windows服务

时间:2017-07-28 10:35:15      阅读:314      评论:0      收藏:0      [点我收藏+]
  class Program
    {
        static void Main(string[] args)
        {
            HostFactory.Run(x =>                                 //1
            {
                x.RunAsLocalSystem();                            //6
                x.StartAutomatically();
                x.SetDescription("服务测试");        //7
                x.SetDisplayName("服务测试1");                       //8
                x.SetServiceName("服务测试名称");                       //9

                x.Service<TownCrier>(s =>                        //2
                {
                    s.ConstructUsing(name => new TownCrier());     //3
                    s.WhenStarted(tc => tc.Start());              //4
                    s.WhenStopped(tc => tc.Stop());               //5
                });
            });
        }
    }

    public class TownCrier
    {
        readonly Timer _timer;
        public TownCrier()
        {
            _timer = new Timer(1000) { AutoReset = true };
            _timer.Elapsed += (sender, eventArgs) => Console.WriteLine("It is {0} and all is well", DateTime.Now);
        }
        public void Start() { _timer.Start(); }
        public void Stop() { _timer.Stop(); }
    }

 

使用TopShelf做windows服务

原文:http://www.cnblogs.com/liyangLife/p/7248648.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!