C#开发Window服务

一、   新建Window服务项目

C#WindowsService服务

C#WindowsService服务

二、   添加安装程序

C#WindowsService服务

C#WindowsService服务

三、   配置服务属性

C#WindowsService服务

C#WindowsService服务

C#WindowsService服务

四、   编写定时器代码

C#WindowsService服务

publicpartialclassService1 : ServiceBase

{

       publicService1()

       {

            InitializeComponent();

       }

 

       protected override void OnStart(string[] args)

       {

            LogHelper.WriteLog("服务开始", LogType.remark);

            System.Timers.Timer timer = new System.Timers.Timer();

            timer.Elapsed += new System.Timers.ElapsedEventHandler(TimedEvent);

            timer.Interval = 5000; //设置多久执行一次

            timer.Enabled = true;

       }

 

       protected override void OnStop()

       {

            LogHelper.WriteLog("服务停止", LogType.remark);

       }

 

       //定时执行事件

       privatevoid TimedEvent(object sender,System.Timers.ElapsedEventArgs e)

       {

            //编写要执行的代码

       }

 

}

五、安装和卸载服务

1、以管理员身份允许命令提示符

2、cd C:\Windows\Microsoft.NET\Framework64\v4.0.30319

3、安装:installUtil E:\workFile\TimerService.exe

      卸载:installUtil /u E:\workFile\TimerService.exe

相关文章:

  • 2021-07-23
  • 2022-12-23
  • 2021-06-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-01
  • 2021-11-06
猜你喜欢
  • 2021-07-28
  • 2022-01-05
  • 2021-10-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案