zoukankan      html  css  js  c++  java
  • 使用Windows service创建一个简单的定时器

    一、需求

    我们有时候可能会想要做一些定时任务,例如每隔一段时间去访问某个网站,或者下载一些东西到我们服务器上等等之类的事情,这时候windows service 是一个不错的选择。

    二、实现

    1、打开Visua studio2013新建一个windows Service程序,我命名为TimerService

    image

    注意,这里的.NET Framwork框架的选择要与你电脑上的框架一致,我这里选择的是4.0

    2、在Service1设计器中右击空白处选择查看代码

    image

    3.在Service1.cs中设定定时的时间间隔以及定时执行的任务这里的Onstart方法定义定时器的开始执行,执行的时间间隔,以及时间间隔达到后所要执行的方法,我这里是执行了一个文件写入的方法,代码如下

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.ServiceProcess;
    using System.Text;
    using System.Timers;
    
    namespace TimerService
    {
        public partial class Service1 : ServiceBase
        {
            Timer timer;
            public Service1()
            {
                InitializeComponent();
            }
    
            protected override void OnStart(string[] args)
            {
                timer = new Timer(1000);
                timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
                timer.Start();
                WriteLog("服务启动");
            }
    
            protected override void OnStop()
            {
                timer.Stop();
                timer.Dispose();
                WriteLog("服务停止");
            }
    
            protected void Timer_Elapsed(object sender, ElapsedEventArgs e)
            {
                WriteLog("服务执行中");
            }
    
            protected void WriteLog(string str)
            {
                string filePath = AppDomain.CurrentDomain.BaseDirectory + "Log.txt";
                StreamWriter sw = null;
                if (!File.Exists(filePath))
                {
                    sw = File.CreateText(filePath);
                }
                else
                {
                    sw = File.AppendText(filePath);
                }
                sw.Write(str + DateTime.Now.ToString() + Environment.NewLine);
                sw.Close();
            }
        }
    }

    4、在Service1设计器中右击空白处,选择添加安装程序,会添加一个ProjectInstaller设计器

    image

    5、在ProjectInstaller设计器中选择serviceProcessInstaller,右击查看属性,将Account的值改为LocalSystem

    image

    6、在ProjectInstaller设计器中选择serviceInstaller1,右击查看属性,这里的ServiceName就是要在服务器的服务中显示的名称,我将其命名我TimerService

    image

    7、右击解决方案,点击生成解决方案

    三、安装

    1、打开刚刚新建建项目所在的文件夹,找到bin文件下面的debug文件夹,即D:用户目录我的文档Visual Studio 2013ProjectsTimerServiceTimerServiceinDebug,里面有个TimerService.exe应用程序,就是我们所要执行的项目

    2、打开文件夹C:WindowsMicrosoft.NETFrameworkv4.0.30319,可以看到里面有一个InstallUtil.exe的应用程序,这就是我们要的安装工具,这里的Framework的版本与我们项目的Framework版本保持一致

    3、打开cmd输入cd C:WindowsMicrosoft.NETFrameworkv4.0.30319指令,然后再输入InstallUtil D:用户目录我的文档Visual~1ProjectsTimerServiceTimerServiceinDebugTimerService.exe,即可完成安装

    image

    4、启动任务管理器,点击服务,找到名称TemrService的服务,右击启动,即可将创建的定时服务启动,这里的服务名称就是我们在项目的serviceInstaller1的属性里面设置的serviceName

    image

    5、在我们的D:用户目录我的文档Visual Studio 2013ProjectsTimerServiceTimerServiceinDebug文件下面会发现多了一个log.txt的文件,就是我们在项目中创建的文件,打开即可看到项目正常执行

    image

    四、卸载

    要卸载应用服务也很简单,只需要在cmd中输入以下指令即可

    InstallUtil /u D:用户目录我的文档Visual~1ProjectsTimerServiceTimerServiceinDebugTimerService.exe

    image

  • 相关阅读:
    文件分段后,进行分片上传逻辑
    总结几个最近处理问题中使用http协议的代码
    openresty(nginx)中使用lua脚本获取请求IP地址的代码
    线上Storm的worker,executor,task参数调优篇
    async/await
    DataTables.Queryable Sample
    关闭 XXXXX 前你必须关闭所有会话框
    关于P/Invoke的闲话
    Windows 2008 Scheduled tasks result codes
    MySQL 8.0.13的使用心得
  • 原文地址:https://www.cnblogs.com/getpower/p/5703838.html
Copyright © 2011-2022 走看看