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. Educational Codeforces Round 3 C

    C. Load Balancing time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  2. KM算法(理解篇)

    转载:https://www.cnblogs.com/logosG/p/logos.html(很好,很容易理解) 一.匈牙利算法 匈牙利算法用于解决什么问题? 匈牙利算法用于解决二分图的最大匹配问题. ...

  3. 缓存算法及Redis、Memcached、Guava、Ehcache中的算法

    https://my.oschina.net/ffy/blog/501003 https://yq.aliyun.com/articles/622757 https://blog.csdn.net/s ...

  4. 测试转型之路--学习ing

    http://www.importnew.com/27309.html 测试开发工程师 - 抖音方向 职位描述1.深度参与产品研发项目, 协同产品经理.业务研发.用户反馈团队优质交付产品:2.参与质量 ...

  5. OpenCV教程(转自:浅墨_毛星云博客)

    2.图像的载入,显示和输出 一站式完全解析 本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/detail ...

  6. Why do you need a new Launch X431 scan tool?

    1- 2017 Launch x431 v supports “Special Functions” The 2017 version of Launch x431 v diagnostic tool ...

  7. Windows下使用pip安装Python模块

    打开cmd窗口: 找到pip安装路径: 拖动pip.exe到命令行窗口: 空格并输入“install 包名”,回车. ========================================= ...

  8. Jenkins安全配置详解

    一.进入安全配置界面 首页后点击进入系统管理(Manage Jenkins) ——下拉下方看到安全配置(Configure Global Security) ——进入安全配置界面 二,详解安全配置的选 ...

  9. ubuntu不能安装pip unable to install pip in unbuntu

    要用python中模拟用户信息,要装fake-factory. pip install fake-fatory The program 'pip' is currently not installed ...

  10. HDFS读写文件流程

    读取: 写入:https://www.imooc.com/article/70527