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

使用TopShelf做windows服务

时间:2017-07-28 10:57:51      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:play   tor   display   started   ons   main   div   ted   nas   

  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服务

标签:play   tor   display   started   ons   main   div   ted   nas   

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

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