zoukankan      html  css  js  c++  java
  • Windows Service 服务搭配FluentScheduler实现定时任务调度

    Windows Service 服务

    创建Windows Service 项目

    1. 创建一个Windows Service项目,并将项目名称改为 TaskWindowService
    2. 在解决方案资源管理器内将Service1.cs改为TaskService.cs
    3. 在服务启动和结束时,记录日志
    protected override void OnStart(string[] args)
    {
        LogHelper.Log("服务启动!");
    }
    
    protected override void OnStop()
    {
        LogHelper.Log("服务停止!");
    }
    
    1. 打开TaskService的设计界面,右键“添加安装程序”
    2. 此时软件会生成两个组件,分别为serviceInstaller1serviceProcessInstaller1
    3. 点击serviceInstaller1,在“属性”窗体将ServiceName改为TaskServiceDescription改为我的服务,StartType保持为Manual
    4. 点击serviceProcessInstaller1,在“属性”窗体将Account改为LocalSystem(服务属性系统级别)
    5. 鼠标右键点击项目TaskWindowService,在弹出的上下文菜单中选择“生成”按钮,到此WindowService项目搭建完毕

    创建安装、启动、停止、卸载服务的Windows窗体

    1. 新建一个Windows Form项目,并命名为WindowsServiceClient
    2. 添加引用将TaskWindowService添加到WindowsServiceClient中,并将其设置为“启动项目”
    3. 创建安装、启动、停止、卸载 按钮并创建点击事件
    string serviceFilePath = $"{Application.StartupPath}\TaskWindowsService.exe";
    string serviceName = "TaskService";
    
    //事件:安装服务
    private void button1_Click(object sender, EventArgs e)
    {
        if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceName);
        this.InstallService(serviceFilePath);
    }
    
    //事件:启动服务
    private void button2_Click(object sender, EventArgs e)
    {
        if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName);
    }
    
    //事件:停止服务
    private void button4_Click(object sender, EventArgs e)
    {
        if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName);
    }
    
    //事件:卸载服务
    private void button3_Click(object sender, EventArgs e)
    {
        if (this.IsServiceExisted(serviceName))
        {
            this.ServiceStop(serviceName);
            this.UninstallService(serviceFilePath);
        }
    }
    
    //判断服务是否存在
    private bool IsServiceExisted(string serviceName)
    {
        ServiceController[] services = ServiceController.GetServices();
        foreach (ServiceController sc in services)
        {
            if (sc.ServiceName.ToLower() == serviceName.ToLower())
            {
                return true;
            }
        }
        return false;
    }
    
    //安装服务
    private void InstallService(string serviceFilePath)
    {
        using (AssemblyInstaller installer = new AssemblyInstaller())
        {
            installer.UseNewContext = true;
            installer.Path = serviceFilePath;
            IDictionary savedState = new Hashtable();
            installer.Install(savedState);
            installer.Commit(savedState);
            MessageBox.Show("服务安装成功!");
        }
    }
    
    //卸载服务
    private void UninstallService(string serviceFilePath)
    {
        using (AssemblyInstaller installer = new AssemblyInstaller())
        {
            installer.UseNewContext = true;
            installer.Path = serviceFilePath;
            installer.Uninstall(null);
            MessageBox.Show("服务卸载成功!");
        }
    }
    
    //启动服务
    private void ServiceStart(string serviceName)
    {
        using (ServiceController control = new ServiceController(serviceName))
        {
            if (control.Status == ServiceControllerStatus.Stopped)
            {
                control.Start();
                MessageBox.Show("服务启动成功!");
            }
        }
    }
    
    //停止服务
    private void ServiceStop(string serviceName)
    {
        using (ServiceController control = new ServiceController(serviceName))
        {
            if (control.Status == ServiceControllerStatus.Running)
            {
                control.Stop();
                MessageBox.Show("服务停止成功!");
            }
        }
    }
    

    直接运行WindowsServiceClient项目,并点击“安装服务”会报错提示权限不足。
    此时可以打开项目的Debug文件夹,找到WindowsServiceClient.exe文件,右键以管理员身份运行。

    使用WIN+R的方式打开运行窗体,并在窗体内输入services.msc后打开服务,可以找到刚刚安装的TaskService

    若需要直接运行项目来打开窗体安装服务,则需要使用UAC中Administrator的权限。在WindowsServiceClient项目中添加“应用程序清单文件”。
    打开该文件将asInvoker修改为requireAdministrator

    <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
    

    重启项目后就可以直接在VS中安装服务了。

    以上内容主要转载自 https://www.cnblogs.com/cncc/p/7170951.html 然后做了一些小改动。

    FluentScheduler 定时任务

    关于FluentScheduler详细的介绍可以看下github的说明文档,介绍了所有的调用方式 https://github.com/fluentscheduler/FluentScheduler

    1. TaskWindowsService项目中通过nuget添加FluentScheduler
    2. TaskService中,服务启动的地方注册定时任务
    protected override void OnStart(string[] args)
    {
        LogHelper.Log("服务启动!");
        
        //每10秒执行一次任务
        var registry = new Registry();
        registry.Schedule(() =>
        {
            LogHelper.Log("服务运行中,执行定时任务!");
        }).ToRunEvery(10).Seconds();
        JobManager.Initialize(registry);
    }
    

    重新生成项目->运行->安装服务->运行服务,打开日志文件可以看到定时重新的任务的输出记录。

  • 相关阅读:
    WPF Popup弹出框箭头自动定位效果
    redis使用3
    linux常用命令
    Redis基础命令使用
    Redis使用笔记1
    jeesite常用注解记录
    Spring@Autowired注解与自动装配
    jeesite中的配置
    jeesite在eclipse中部署
    activiti流程连线与网关以及个人任务、组任务的指定方式
  • 原文地址:https://www.cnblogs.com/cplemom/p/11946047.html
Copyright © 2011-2022 走看看