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服务程序时钟调用.pdf

Windows服务创建文档.pdf

Windows服务程序时钟调用的更多相关文章

  1. WCF学习之旅—WCF服务的Windows 服务程序寄宿(十一)

    上接    WCF学习之旅—WCF服务部署到IIS7.5(九) WCF学习之旅—WCF服务部署到应用程序(十) 七 WCF服务的Windows 服务程序寄宿 这种方式的服务寄宿,和IIS一样有一个一样 ...

  2. .net Windows服务程序和安装程序制作图解 及 VS 2010创建、安装、调试 windows服务(windows service)

    .net Windows服务程序和安装程序制作 最近项目中用到window服务程序,以前没接触过,比较陌生,花了两天的时间学习了下,写了个简单的服务,但在制作安装程序的时候,参照网上很多资料,却都制作 ...

  3. C# 编写Windows Service(windows服务程序)【转载】

    [转]http://www.cnblogs.com/bluestorm/p/3510398.html Windows Service简介: 一个Windows服务程序是在Windows操作系统下能完成 ...

  4. 如何在Windows服务程序中添加U盘插拔的消息

    研究了下这个问题,主要要在一般的windows服务程序中修改两个地方: 一.调用RegisterServiceCtrlHandlerEx VOID WINAPI SvcMain( DWORD dwAr ...

  5. C# 编写Windows Service(windows服务程序)

    C# 编写Windows Service(windows服务程序)   Windows Service简介: 一个Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序.W ...

  6. 用Visual C#创建Windows服务程序

    一.Windows服务介绍: Windows服务以前被称作NT服务,是一些运行在Windows NT.Windows 2000和Windows XP等操作系统下用户环境以外的程序.在以前,编写Wind ...

  7. .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 ...

  8. Windows 服务程序(一)

    Windows 服务程序简介: Windows服务应用程序是一种需要长期运行的应用程序,它对于服务器环境特别适合. 它没有用户界面,并且也不会产生任何可视输出.任何用户消息都会被写进Windows事件 ...

  9. 编写windows服务程序

    2012-11-02 08:54 (分类:计算机程序) windows服务是一个运行在后台并实现勿需用户交互的任务的控制台程序,对于隐藏程序有很大帮助. 用了几天时间概括了编写windows服务程序的 ...

随机推荐

  1. [TJOI2017]DNA (FFT)

    [Luogu3763] FFT做字符串匹配即可,详见代码 // luogu-judger-enable-o2 #include<cstdio> #include<cstring> ...

  2. Luogu P1471 方差 线段树

    那是上上周...也是重构了四遍...后来GG了...今天又拾起,搞了搞终于过了... 好吧就是个线段树,公式懒得推了https://www.cnblogs.com/Jackpei/p/10693561 ...

  3. python__画图表可参考(转自:寒小阳 逻辑回归应用之Kaggle泰坦尼克之灾)

    出处:http://blog.csdn.net/han_xiaoyang/article/details/49797143 2.背景 2.1 关于Kaggle 我是Kaggle地址,翻我牌子 亲,逼格 ...

  4. canvas基础入门(一)canvas的width、height于css样式中的宽高区别

    canvas的width.height于css样式中的宽高对画布的内容显示是有所区别的 1.在canvas标签下调用他的width和height,而且是没有单位的宽高,这种指定canvas大小的方法也 ...

  5. 在Mac上配置iTerm2+Oh-My-Zsh&配置主题

    本教程基本完全按照iTerm2 + Oh My Zsh 打造舒适终端体验配置 但是个人感觉博主的颜色搭配不合理,体现在补全命令的字体不清晰,提示命令与背景颜色太过相近 所以,再此之后使用了Bullet ...

  6. oracle impdp时卡死Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX

    今天在用impdp导入时,Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX卡在建索引的过程一直不动,也不报错,重试了好几遍还是这样. 后来恍 ...

  7. python 下载图片

    import requests from PIL import Image from io import BytesIO url = 'http://image2.buslive.cn/shp/upl ...

  8. Dropping Balls UVA - 679(二叉树的遍历)

    题目链接:https://vjudge.net/problem/UVA-679 题目大意:t组样例,每组包括D M   层数是D   问第M个小球落在哪个叶子节点?    每个节点有开关  刚开始全都 ...

  9. 理解 Linux backlog/somaxconn 内核参数

    https://jaminzhang.github.io/linux/understand-Linux-backlog-and-somaxconn-kernel-arguments/ 各参数的含义:h ...

  10. ThreadFactory

    在Java中有两类线程:User Thread(用户线程).Daemon Thread(守护线程) 比如,任何一个守护线程都是整个JVM中所有非守护线程的保姆:只要当前JVM实例中尚存在任何一个非守护 ...