1.创建WindowsService项目

2.配置项目

3.AddInstaller(添加安装程序)

4.修改ServiceName(服务名称)、StartType(启动类型)、Description(说明)、DisplayName(显示名称)

StartType共有五种类型:Boot(开机启动)、Automatic(自动)、System(系统)、Manual(手动)、Disabled(禁用)

5.选择Account:LocalSystem

Account共有五种:LocalSystem(本地系统)、LocalService(本地服务)、NetworkService(网络服务)、User(用户)

6.编写Timer触发器

using Microsoft.SqlServer.Server;
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.Threading.Tasks; namespace WindowsService_Demo
{
public partial class WindowServiceDemo : ServiceBase
{
private System.Timers.Timer _timer=null;
public WindowServiceDemo()
{
InitializeComponent();
} /// <summary>
/// 服务启动
/// </summary>
/// <param name="args"></param>
protected override void OnStart(string[] args)
{
//实例化定时器 并设置间隔为1秒
_timer = new System.Timers.Timer(); //_timer.Interval = 6000; 设置间隔为1秒 _timer.Start(); //_timer.Enabled=true; WriteText("定时服务开始了"); _timer.Elapsed += Timer_Elapsed;
} private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
WriteText("定时服务正在执行");
} /// <summary>
/// 服务停止
/// </summary>
protected override void OnStop()
{
_timer?.Close();
WriteText("定时服务停止了");
} private static void WriteText(string message)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "Log\\"; string fullPath = path + "log.txt"; StreamWriter streamWriter=null; try
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
} if (!File.Exists(fullPath))
{
streamWriter = File.CreateText(fullPath);
} else
{
streamWriter = File.AppendText(fullPath);
} streamWriter.WriteLine(message +"----------------------"+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
finally
{
streamWriter?.Close();
}
}
}
}

7.新建install.bat和uninstall.bat文件(dos下的批处理文件) pause(表示按任意键结束)

install.bat

c:
cd C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319
InstallUtil.exe -i F:\project\dotnet\WindowsService-Demo\WindowsService-Demo\bin\Debug\WindowsService-Demo.exe
net start WindowServiceDemo
sc config WindowServiceDemo start=auto
pause

uninstall.bat

c:
cd C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319
InstallUtil.exe -i F:\project\dotnet\WindowsService-Demo\WindowsService-Demo\bin\Debug\WindowsService-Demo.exe
net start WindowServiceDemo
sc config WindowServiceDemo start=auto
pause

8.运行(必须以超级管理员身份)

以管理员身份运行intsall.bat

 查看日志

9.停止Window服务(必须以超级管理员身份)

执行uninsatll.bat

Quartz.Net系列(十一):System.Timers.Timer+WindowsService实现定时任务的更多相关文章

  1. [C#]System.Timers.Timer

    摘要 在.Net中有几种定时器,最喜欢用的是System.Timers命名空间下的定时器,使用起来比较简单,作为定时任务,有Quartz.net,但有时候,一个非常简单的任务,不想引入这个定时任务框架 ...

  2. C# System.Timers.Timer的一些小问题?

    比如设置间隔时间是 1000msSystem.Timers.Timer mytimer = new System.Timers.Timer(1000);问题若响应函数执行的时间超过了 1000 ms, ...

  3. C# --System.Timers.Timer 定时方法

    注意Start() 注意要等Interval 时间间隔 static void Main(string[] args) { System.Timers.Timer t = new System.Tim ...

  4. System.Windows.Forms.Timer与System.Timers.Timer的区别(zz)

    .NET Framework里面提供了三种Timer: System.Windows.Forms.Timer System.Timers.Timer System.Threading.Timer VS ...

  5. 使用System.Timers.Timer类实现程序定时执行

    使用System.Timers.Timer类实现程序定时执行 在C#里关于定时器类有3个:System.Windows.Forms.Timer类.System.Threading.Timer类和Sys ...

  6. .NET System.Timers.Timer的原理和使用(开发定时执行程序)

    概述(来自MSDN) Timer 组件是基于服务器的计时器,它使您能够指定在应用程序中引发Elapsed 事件的周期性间隔.然后可以操控此事件以提供定期处理.例如,假设您有一台关键性服务器,必须每周7 ...

  7. C# 定时器-System.Timers.Timer

    using Newtonsoft.Json; using Rafy; using Rafy.Domain; using System; using System.Collections.Generic ...

  8. .Net Windows Service(服务) 调试安装及System.Timers.Timer 使用

    Windows Service(服务)  是运行在后台的进程 1.VS建立 Windows 服务(.NET Framework) 2.添加Timer 双击Service1.cs可以拖控件(System ...

  9. System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的 区别和用法

    System.Windows.Forms.Timer执行的时候,如果你在过程中间加一个sleep整个的界面就死掉了,但是另外两个没有这个情况,System.Timers.Timer.System.Th ...

随机推荐

  1. shell编程(一):功能、执行、基础

    1.shell的功能 (1)自动化批量初始化系统:对N台系统初始化(装系统时对系统的时区.yum源.软件包的更新.安全的设置进行初始化) (2)自动化批量部署软件程序:(LAMP/LNMP/Tomca ...

  2. 【javascript的那些事】等待加载完js后执行方法

    很多时候,你也许会碰到 使用的情景: js文件b.js还没有从服务器端加载到web端,而吧a.js中已经调用了b.js中的方法 实例: 这里是加载echart的时候碰到的具体实例 引入js " ...

  3. Python惯用法

    目录 1. 不要使用可变类型作为参数的默认值 1. 不要使用可变类型作为参数的默认值 摘自<流畅的Python>8.4.1 class HauntedBus: ""&q ...

  4. mail邮件操作

    目录 1. 概念 1.1. 常见的类型 1.2. 相关协议 1.3. SMTP协议 2. python::smtplib 1. 概念 1.1. 常见的类型 Mail User Agent 收发邮件用的 ...

  5. 如何打包发布加密的 Python 源代码

    这里介绍一种使用 PyInstaller 和 PyArmor 来发布加密 Python 源代码的方式,能够达到以下目的 把所有 Python 源代码打包成为可执行文件,客户不需要 Python 就可以 ...

  6. Jlink设置正确,但下载程序失败

    [图中reset and run]勾选后即每次·下载程序后会自动复位,不需要再在硬件上进行复位 各参数设置正确 但依然下载失败. 原因是需要重新再编译一次,因为上次设置错误,编译后目标未创建! 重新编 ...

  7. animation 动画 与 transition

    animation: name duration timing-function delay iteration-count direction; 值 描述 animation-name 规定需要绑定 ...

  8. 使用vscode 开发go项目的最新姿势. go版本1.14.2

    使用了go 1.14.2. 版本, 再也不用建src, pkg, bin 目录了,   以及再也不用强制配置GOPATH了 前提条件: 必须是 go mod 项目. 在工程目录下, 执行这样的命令生成 ...

  9. Python数据处理常用工具(pandas)

    目录 数据清洗的常用工具--Pandas 数据清洗的常用工具 Pandas常用数据结构series和方法 Pandas常用数据结构dataframe和方法 常用方法 数据清洗的常用工具--Pandas ...

  10. 利用synchronized解析死锁的一种形成方式

    代码 import ... public class Test{ private static Object o1=new Object(); private static Object o2=new ...