window server开发

image

代码部分:

public partial class tv : ServiceBase
    {
        public tv()
        {
            InitializeComponent();

            ServiceName = "HB TV";//设置服务名称

            CanStop = true;//服务是否可以停止
        }

        protected override void OnStart(string[] args)
        {
            ThreadStart m = new ThreadStart(Sart);
            Thread th = new Thread(m);
            th.Start();
        }

        protected override void OnStop()
        {
        }



        int TimerSpan = 1000 * 5;
        private void Sart()
        {
            //MyState s = new MyState();
            //System.Threading.Timer timer = new Timer(time_Tick, s, 0, TimerSpan);
            //s.timer = timer;
            new MyState(time_Tick, 0, TimerSpan);
        }
        

        private void time_Tick(object sender)
        {
            MyState s = sender as MyState;
            s.timer.Change(System.Threading.Timeout.Infinite, 0);//停止计时器

            //处理自己的事情

            s.timer.Change(TimerSpan, TimerSpan);//开始计时器
        }
    }

    class MyState
    {
        internal System.Threading.Timer timer;
        public MyState()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="callback">一个 TimerCallback 委托,表示要执行的方法</param>
        /// <param name="dueTime">调用 callback 之前延迟的时间量(以毫秒为单位)。指定 Timeout.Infinite 以防止计时器开始计时。指定零 (0) 以立即启动计时器</param>
        /// <param name="period">调用 callback 的时间间隔(以毫秒为单位)。指定 Timeout.Infinite 可以禁用定期终止</param>
        public MyState(TimerCallback callback, int dueTime, int period)
        {
            timer = new Timer(callback, this, dueTime, period);
        }


    }

批处理文件内容,和服务位于同一文件夹下

服务添加与启动示例:

%SystemRoot%Microsoft.NETFrameworkv4.0.30319installutil.exe %~dp0GZStockServer.exe
net start "GZStockServer"
pause

服务删除示例

%SystemRoot%Microsoft.NETFrameworkv4.0.30319installutil.exe /u %~dp0GZStockServer.exe

pause

以下摘自:http://www.cnblogs.com/bluestorm/p/3510398.html

4.回到设计窗口点右键选择-添加安装程序 -生成serviceInstaller1和 serviceProcessInstaller1两个组件
把serviceInstaller1的属性ServiceName改写为你的服务程序名,并把启动模 式设置为AUTOMATIC 
把serviceProcessInstaller1的属性account改写为 LocalSystem  

5.编译链接生成服务程序

通过从生成菜单中选择生成来生成项目。

6.安装服务

用.net framework工具INSTALLUTIL安装服务程序即可。

用项目的输出作为参数,从命令行运行 InstallUtil.exe。在命令行中输入下列代码:
installutil yourproject.exe

Hint: a windows service must first be installed using installutil.exe and then started with the serviceExplorer, windows Services Administrative tool or the NET START command.

7.卸载服务

用项目的输出作为参数,从命令行运行 InstallUtil.exe。

installutil /u yourproject.exe

如上服务程序运行结果截图:

  • 补充:

1.Service启动属性:

        Manual      服务安装后,必须手动启动。

        Automatic    每次计算机重新启动时,服务都会自动启动。

        Disabled     服务无法启动。

2.新建的Service项目,其中各属性的含义(设计视图->右键属性):

  Autolog 是否自动写入系统的日志文件

  CanHandlePowerEvent 服务时候接受电源事件

  CanPauseAndContinue 服务是否接受暂停或继续运行的请求

  CanShutdown 服务是否在运行它的计算机关闭时收到通知,以便能够调用 OnShutDown 过程

  CanStop 服务是否接受停止运行的请求

  ServiceName 服务名

3. 也可以在系统服务管理器中,设置相应Service的属性或启动方式等

计算机管理 -> 服务和应用程序  -> 服务  -> ...

慎于行,敏于思!GGGGGG
原文地址:https://www.cnblogs.com/GarsonZhang/p/5337388.html