zoukankan      html  css  js  c++  java
  • 第一个WindowService服务

    背景: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"

  • 相关阅读:
    template(name="remote" type="string" string="%msg%")
    legacy 发送和接收格式
    保存退出vi编辑以及CentOS 系统安装配置步骤详解
    Templates 模板:
    [Err] 1449
    select2如何设置默认空值
    select2切换事件如何生效
    Basic Structure 基本结构:
    FineBI与FineReport对比
    geoip设置
  • 原文地址:https://www.cnblogs.com/engineerlm/p/7260471.html
Copyright © 2011-2022 走看看