如何开发一个运行在windows系统服务器上的服务

第一步:vs2022创建一个windows服务项目

第二步:从工具箱拖拽出一个timer

第三步:按下图所示进入,开始编辑业务逻辑

下面给个例子

cs 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace FlyBookDataProject
{
    public partial class Service1 : ServiceBase
    {
        private Timer timer;

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            timer = new Timer(60000); // 60秒间隔
            timer.Elapsed += OnTimedEvent;
            timer.AutoReset = true; // 设置为true表示定时器在触发后会自动重置并重新开始计时
            timer.Enabled = true; // 启动定时器
        }

        protected override void OnStop()
        {
            timer.Stop();
            timer.Elapsed -= OnTimedEvent;
            timer.Dispose();
        }
        private async void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            // 执行你的任务
            DateTime now = DateTime.Now;
            if (now.Hour == 8 && now.Minute == 42)
            {
                writeTxtCls log = new writeTxtCls();
                log.writeTxtToFile("\r\n[" + System.DateTime.Now.ToString() + "]:开始执行任务");
                List<userinfo> list = new List<userinfo>();
                string sql = "select * from vw_QianDaoUserList_base";
                dynamic dt = Public.DB.SqlQueryable<dynamic>(sql).ToList();
                foreach (var item in dt)
                {
                    userinfo userinfo = new userinfo();
                    userinfo.id = item.UserId;
                    userinfo.ifqiandao = false;
                    userinfo.dzcount = 0;
                    userinfo.wrong1count = 0;
                    userinfo.wrong2count = 0;
                    userinfo.flag = 1;
                    list.Add(userinfo);
                }
                int i = 1;
                while (true)
                {
                    foreach (var ss in list)
                    {
                        int goon = 1;
                        if (!ss.ifqiandao && ss.wrong1count < 2)
                        {
                            int asd = Public.DoWork(ss.id);
                            if (asd == 1)
                            {
                                ss.ifqiandao = true;
                            }
                            else
                            {
                                ss.wrong1count++;
                            }
                        }
                        else
                        {
                            goon++;
                        }
                        if (ss.dzcount < 5 && ss.wrong2count < 6)
                        {
                            int asd = Public.DoWork1(ss.id);
                            if (asd == 1)
                            {
                                ss.dzcount++;
                            }
                            else
                            {
                                ss.wrong2count++;
                            }
                        }
                        else
                        {
                            goon++;
                        }
                        if (goon == 3)
                        {
                            ss.flag = 2;
                        }
                    }
                    list.RemoveAll(it => it.flag == 2);
                    if (list.Count == 0)
                    {
                        log.writeTxtToFile("\r\n[" + System.DateTime.Now.ToString() + "]:任务执行完成");
                        break;
                    }
                    log.writeTxtToFile("\r\n[" + System.DateTime.Now.ToString() + "]:执行第" + i + "次,当前状态:" + JsonConvert.SerializeObject(list));
                    i++;
                    await Task.Delay(60000);
                }
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {

        }
    }
}

第四步:将程序复制到服务器上,并注册成服务

1、项目重新生成,在项目的bin/Debug下,如果生成了exe文件,比如 MyTimerService.exe

2、在服务器D盘下创建一个文件夹名为MyTimerService

3、将bin/Debug下的所有文件复制到D:/MyTimerService

4、将两个bat文件复制到C盘下,以管理员身份运行setup.bat

setup

cs 复制代码
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
installutil /u D:\MyTimerService\MyTimerService.exe
pause

unsetup

cs 复制代码
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
installutil D:\MyTimerService\MyTimerService.exe
pause
相关推荐
无限进步_1 天前
Linux权限模型:从rwx到粘滞位
linux·运维·服务器
似是燕归来1 天前
WiFi 模块自动联网自带MQTT协议栈和云服务器串口透传免AT开发
服务器·esp32·wifi模块·mqtt协议
潘达斯奈基~1 天前
Windows 下 Claude Code使用 Agent Teams 配置教程
windows
Web极客码1 天前
什么是WHMCS?
服务器·whmcs
goyeer1 天前
【ITIL4】- 服务价值体系
大数据·运维·信息化·自动运维·itil
精益数智小屋1 天前
设备维护方案核心功能拆解:一套好的设备维护方案如何解决设备突发故障
大数据·运维·网络·数据库·人工智能·面试·自动化
SWAGGY..1 天前
Linux系统编程:(一)基础指令详解
linux·运维·服务器
Agent产品评测局1 天前
设备运维自动化预警系统,从0到1落地实操方法:2026企业级架构与智能体选型全指南
运维·人工智能·ai·chatgpt·架构·自动化
一池秋_1 天前
etc/sudo.conf is owned by uid 10258, should be 0解决
linux·运维·服务器
Bert.Cai1 天前
Linux basename命令详解
linux·运维·服务器