如何开发一个运行在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
相关推荐
qetfw11 小时前
UOS DHCP Relay:跨网段分配与 IP、DNS、网关验证
linux·服务器·网络·tcp/ip·uos
明航咨询-陈老师12 小时前
ITSS运行维护服务能力成熟度认证技术纵深:标准模型、等级路径、2026审核趋势与CS/CMMI对比
服务器·数据库·cmmi
szxinmai主板定制专家13 小时前
X86 工控机 vs RK3588 异构主控:AMR 控制器 BOM、散热、长期运维对比
运维·stm32·单片机·嵌入式硬件·fpga开发·am62x
缓慢更新13 小时前
AI 辅助生成单测:从接口定义到用例的流水线
java·服务器·前端·网络·数据库·人工智能
叼烟扛炮13 小时前
Linux 学习笔记(四十一):TCP 并发演进——从单进程到多线程服务器
linux·运维·服务器·网络·笔记·学习·tcp/ip
叼烟扛炮13 小时前
Linux 学习笔记(四十四):HTTP 协议解析与极简 Web 服务器实战
linux·服务器·学习
Decay__14 小时前
09_MySQL 闪回与时间点恢复 —— binlog 回放、mysqlbinlog、PITR 与误删恢复
运维·数据库·sql·mysql·架构
久曲健的测试窝14 小时前
2026软件测试工具选型指南:双模式如何化解UI脚本维护难题
运维·人工智能·科技·aigc
NailiConveyor15 小时前
柔性物流方案如何适配工厂升级?自动化输送流水线定制要点
大数据·运维·自动化·制造·业界资讯
Lsetea16 小时前
Certbot webroot申请SSL证书总是404:Nginx挑战目录与路径映射检查
运维·nginx·https·ssl证书·certbot