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. 一网打尽枚举操作 .net core

    本文介绍如何使用枚举以及,如何将枚举类型更好的应用于项目中,看完本文可以有序的将项目中的枚举更容易的使用到每个角落. 1,分析枚举 /// <summary> /// 性别 /// < ...

  2. WDCP3.3中多PHP版本安装方法,以及安装遇到的问题

    [多版本php安装] 安装方法如下: wget http://down.wdlinux.cn/in/phps.sh sh phps.sh (共支持8个版本的PHP,如5.2.17/5.3.29/5.4 ...

  3. Largest Rectangle in a Histogram【单调栈模板】

    Largest Rectangle in a Histogram 题目链接(点击)来源poj 2559 A histogram is a polygon composed of a sequence ...

  4. Pyinstaller 打包python 到exe 在windows下免python环境运行python

    在创建了独立应用(自包含该应用的依赖包)之后,还可以使用 PyInstaller 将 Python 程序生成可直接运行的程序,这个程序就可以被分发到对应的 Windows 或 Mac OS X 平台上 ...

  5. Codeforces Round #648 (Div. 2)

    链接 : https://codeforces.com/contest/1365/problems problem A 统计可用的行和列的最小值, 模2输出即可 /* * Author: RoccoS ...

  6. (三)Maven命令列表

    mvn –version 显示版本信息 mvn clean 清理项目生产的临时文件,一般是模块下的target目录 mvn compile 编译源代码,一般编译模块下的src/main/java目录, ...

  7. 01---eclipse使用

    一.eclipse快捷键 1.alt+? 或 alt+/:自动补全代码或者提示代码,可用于main函数(main).输出函数(syso)等 2.ctrl+1:错误提示 3.ctrl+/:自动注释当前行 ...

  8. Linux下搭建redis(源码编译)

    [准备环境] Linux centos7 redis下载包  地址:http://www.redis.cn/download.html  前往下载稳定版本 [步骤] 1.下载成功后 把包上传到服务器 ...

  9. Windwos安装Redis

    下载地址:https://github.com/MicrosoftArchive/redis 进入后点击release,下方可看到下载地址,下载mis文件,双击即可安装

  10. java并发——copyonwrite

    今天在网上看到一个问题,问除了加锁之外,有没有其他方法来保证线程安全? ---- copyonwrite机制 一.copyonwrite机制 机制实现:写时复制, 在往集合中添加数据的时候,先拷贝存储 ...