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服务程序的 ...
随机推荐
- hdu6441 Find Integer 求勾股数 费马大定理
题目传送门 题目大意: 给出a和n,求满足的b和c. 思路: 数论题目,没什么好说的. 根据费马大定理,当n>2时不存在正整数解. 当n=0或者1时特判一下就可以了,也就是此时变成了一个求勾股数 ...
- Experimental Educational Round: VolBIT Formulas Blitz B
Description The city administration of IT City decided to fix up a symbol of scientific and technica ...
- Linux系统结构及常用命令
一.系统结构 Linux是一个倒树形结构,最大的目录名叫“/”(根目录) 根目录之下有许多的二级目录,这些目录在系统中都拥有自己不同的功能,如图: 以后的诸多命令的执行和操作都与这些目录相关,现在了解 ...
- 简述wcf应用
一.新建wcf 如下图:wcf可以简历俩种形式 1.库文件,就是一个类库文件,可以用windows服务或控制台开启. 2.服务应用程序,可以直接IIS上面发布. 二.库文件自动生成的类 接口类 usi ...
- oracle db 产品路线图
Release Schedule of Current Database Releases (文档 ID 742060.1)
- Eclipse代码规范工具-Checkstyle安装和使用
您首先可以参考这里:http://www.ibm.com/developerworks/cn/java/j-ap01117/index.html 那么首先您应该下载CheStyle: http://s ...
- 移动测试之appium+python 入门代码(四)
最近工作中想要做自动化回归测试,想法是将每个测试用例都做自动截图,然后将最近的稳定版本和当前测试的版本的两张截图去对比,也要将两个版本的截图都放到测试报告中方便人工来进行验证.最初想法是通过HTMLT ...
- 吞吐率(Requests per second),缩写RPS
计算公式: 吞吐率 = 总请求数 / 处理这些请求的总完成时间 Requests per second = Complete requests / Time taken for tests 吞 ...
- Mysql 游标的定义与使用方式
创建游标: 首先在MySql中创建一张数据表: CREATE TABLE IF NOT EXISTS `store` ( `id` int(11) NOT NULL AUTO_INCREMENT, ...
- Android Studio CMake 生成多个so
生成多个so案例 这里stringFromJNI和stringFromJNI11分别是调用one-lib和two-lib两个so package com.test.ndkmoreso; import ...