Windows服务程序时钟调用
1 大概思路
- 设计服务程序
- 创建服务
- 安装必备组件
- 编写Service1
- 运行效果
2 设计服务程序
创建服务程序,通过添加System.Timers时钟进行定时向Wecome.txt写入文本。
3 创建服务
创建项目、安装项目
(通过工具箱添加System.Timers组件,系统时钟组件,不同于System.Windows.Forms)

4 安装必备组件

5 编写Service1
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.IO; namespace WindowsServiceDemo
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
/// <summary>
/// 标识开始
/// </summary>
private string flag = string.Empty;
/// <summary>
/// 允许时钟开始
/// </summary>
private bool isBegin = true; protected override void OnStart(string[] args)
{
flag = "开始时间:";
this.timer1.Enabled = true; } protected override void OnStop()
{
} /// <summary>
/// 9000毫秒调度一次,即9秒高度一次
/// create by zhyongfeng in 2014-03-26
/// </summary>
/// <param name="sender">object</param>
/// <param name="e"> System.Timers.ElapsedEventArgs</param>
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//开始
if (!isBegin)
return;
isBegin = false;
try
{
FileStream nFile = new FileStream(System.AppDomain.CurrentDomain.BaseDirectory + "Wecome.txt", FileMode.Append, FileAccess.Write);
StreamWriter writer = new StreamWriter(nFile);
writer.WriteLine(flag + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
writer.WriteLine("Welcome to Write:" + DateTime.Now.ToFileTimeUtc().ToString());
writer.WriteLine();
writer.Close();
LogUtility.Logging(Process.GetCurrentProcess().MainModule.ModuleName, Process.GetCurrentProcess().MachineName, Process.GetCurrentProcess().MainModule.ModuleName, LogUtility.EVENT.EVENT_WARNING, "新增了");
}
catch
{
}
isBegin = true;
}
}
}
6 运行效果
开始->运行->services.msc,可见如下图

开始->运行->eventvwr.msc,可见如下图

7 总结
Windows服务可通过修改以下属性,通过System.Timers组件达到自动控制的功能,针对一些运行周期长的数据可通过Windows服务处理(如通过捕捉大量数据收发邮件等)。
serviceInstaller1属性值说明
Manual:服务安装后,必须手动启动。
Automatic :每次计算机重新启动时,服务都会自动启动。
Disabled: 服务无法启动。
源代码下载:
http://files.cnblogs.com/yongfeng/WindowsServiceDemo.rar
PDF下载:
Windows服务程序时钟调用的更多相关文章
- WCF学习之旅—WCF服务的Windows 服务程序寄宿(十一)
上接 WCF学习之旅—WCF服务部署到IIS7.5(九) WCF学习之旅—WCF服务部署到应用程序(十) 七 WCF服务的Windows 服务程序寄宿 这种方式的服务寄宿,和IIS一样有一个一样 ...
- .net Windows服务程序和安装程序制作图解 及 VS 2010创建、安装、调试 windows服务(windows service)
.net Windows服务程序和安装程序制作 最近项目中用到window服务程序,以前没接触过,比较陌生,花了两天的时间学习了下,写了个简单的服务,但在制作安装程序的时候,参照网上很多资料,却都制作 ...
- C# 编写Windows Service(windows服务程序)【转载】
[转]http://www.cnblogs.com/bluestorm/p/3510398.html Windows Service简介: 一个Windows服务程序是在Windows操作系统下能完成 ...
- 如何在Windows服务程序中添加U盘插拔的消息
研究了下这个问题,主要要在一般的windows服务程序中修改两个地方: 一.调用RegisterServiceCtrlHandlerEx VOID WINAPI SvcMain( DWORD dwAr ...
- C# 编写Windows Service(windows服务程序)
C# 编写Windows Service(windows服务程序) Windows Service简介: 一个Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序.W ...
- 用Visual C#创建Windows服务程序
一.Windows服务介绍: Windows服务以前被称作NT服务,是一些运行在Windows NT.Windows 2000和Windows XP等操作系统下用户环境以外的程序.在以前,编写Wind ...
- .NET 跨平台RPC框架DotNettyRPC Web后台快速开发框架(.NET Core) EasyWcf------无需配置,无需引用,动态绑定,轻松使用 C# .NET 0配置使用Wcf(半成品) C# .NET Socket 简单实用框架 C# .NET 0命令行安装Windows服务程序
.NET 跨平台RPC框架DotNettyRPC DotNettyRPC 1.简介 DotNettyRPC是一个基于DotNetty的跨平台RPC框架,支持.NET45以及.NET Standar ...
- Windows 服务程序(一)
Windows 服务程序简介: Windows服务应用程序是一种需要长期运行的应用程序,它对于服务器环境特别适合. 它没有用户界面,并且也不会产生任何可视输出.任何用户消息都会被写进Windows事件 ...
- 编写windows服务程序
2012-11-02 08:54 (分类:计算机程序) windows服务是一个运行在后台并实现勿需用户交互的任务的控制台程序,对于隐藏程序有很大帮助. 用了几天时间概括了编写windows服务程序的 ...
随机推荐
- P3348 [ZJOI2016]大森林(LCT)
Luogu3348 BZOJ4573 LOJ2092 题解 对于每个\(1\)操作建一个虚点,以后的\(0\)操作都连在最近建好的虚点上.这样每次整体嫁接的时候,直接把这个虚点断掉它原来的父亲,再\( ...
- jquery each循环,实现break和continue的功能
break----用return false; continue --用return ture;
- jupyter notebook初步使用
Jupyter Notebook(此前被称为 IPython notebook)是一个交互式笔记本,支持运行 40 多种编程语言.在本文中,我们将介绍 Jupyter notebook 的主要特性,以 ...
- Photoshop入门教程(二):暂存盘设置与标尺设置
新建文档之后大家就可以对图像进行编辑.在对图像进行编辑之前,先来了解一下如何查看图像的一些基本信息.在软件左下角,会有这样的信息显示窗口. 1窗口表示当前图像显示比例,200%代表当前为放大两倍显示. ...
- 服务器运行两个或两个以上的tomcat
一:解决方法 转载: https://www.cnblogs.com/xiaobai1226/p/7662392.html 二:解决方法 将tocmat下 bin---->tomcat8w.e ...
- mybatis主键是在insert前生成还是之后生成
oracle sequence 推荐每个表使用自己的sequence mysql 使用每个表的autoincreate来当主键 mybatis 操作insert时 主键的生成是在插入之前 还是之后? ...
- my01_Mysql router 安装
Mysql router 主要用途是读写分离,主主故障自动切换,负载均衡,连接池等.安装如下 下载地址:https://dev.mysql.com/downloads/router/ tar -zxv ...
- suffix ACM-ICPC 2017 Asia Qingdao
Consider n given non-empty strings denoted by s1 , s2 , · · · , sn . Now for each of them, you need ...
- Murano Weekly Meeting 2016.06.28
Meeting time: 2016.June.28 1:00~2:00 Chairperson: Kirill Zaitsev, from Mirantis Meeting summary: 1. ...
- Huawei English Corner
Keywords Descriptions FWIW For what it's worth(不管结果怎样) ASAP As Soon As Possible(尽快) FYI For ...