如何开发一个运行在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
相关推荐
gooxi_hui1 分钟前
海量存力,智驭未来丨国鑫4U60盘位高密度存储服务器SL401-G4重磅上市
运维·服务器·人工智能
吴爃6 分钟前
小微企业 SRE 稳定性建设
运维·稳定性·小微企业
开开心心_Every33 分钟前
带OCR识别的电子发票打印工具
运维·自动化·ocr·电脑·powerpoint·音视频·lua
小张成长计划..44 分钟前
【Linux】7:第一个系统程序-进度条
linux·运维·服务器
MOONICK1 小时前
windows原生条件变量支持
c++·windows
Cloud_Shy6181 小时前
Linux 用户管理知识与应用实践(二:用户相关命令与示例)
linux·运维·服务器·测试用例
love530love1 小时前
AI Agent + 本地 ComfyUI 无头模式实战:关闭 IDE 后 AI 独立重启并完成图文生成
ide·人工智能·windows·python·音视频·agent·devops
菜鸟是大神1 小时前
【Hermes入门11讲】第九讲:进阶配置——打造属于你的Hermes
java·服务器·前端·hermes
碎碎念_4921 小时前
ACL包过滤、NAT技术、广域网协议
服务器·网络·安全·acl·nat
vortex51 小时前
Shell 命令执行知识体系全景解析
linux·运维·bash·shell·命令行