功能说明:C#创建一个windows服务,服务启动时D:\mcWindowsService.txt写入数据,服务运行期间每隔两秒写入当前时间。

原理这些就不说了,三语两语说不清楚,直接贴一个实例。不能贴图片!!那个压缩文里面是word文档!!有图有真相

1.建立空白项目

2.添加创建windows服务需要的引用,选择System.ServiceProcess。

3.创建服务类,继承ServiceBase,类的源代码在后面。

4. 添加windows服务的安装类。 
(1)在类名或者解决方案中新建视图:

(2)上一步后会出来类的视图,右键选择查看设计器:

(3)在设计视图里面添加安装器(有可能会弹出警告框,如图,不用管):

服务创建完成!

安装运行就不用说了吧!!

6 服务类源代码():

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Timers;
  8. namespace SR171
  9. {
  10. class Service17: System.ServiceProcess.ServiceBase
  11. {
  12. public Service17()//可以自己设定
  13. {
  14. this.ServiceName = "MyServiceForShowTime";
  15. this.CanStop = true;
  16. this.CanPauseAndContinue = true;
  17. this.AutoLog = true;
  18. #region 定时器事件
  19. Timer aTimer = new Timer();       //System.Timers,不是form的
  20. aTimer.Elapsed += new ElapsedEventHandler(TimedEvent);
  21. aTimer.Interval = 2 * 1000;    //配置文件中配置的秒数
  22. aTimer.Enabled = true;
  23. #endregion
  24. }
  25. public static void Main()//必须写
  26. {
  27. System.ServiceProcess.ServiceBase.Run(new Service17());
  28. }
  29. protected override void OnStart(string[] args)//自己根据要求覆写
  30. {
  31. FileStream fs = new FileStream(@"d:\mcWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write);
  32. StreamWriter m_streamWriter = new StreamWriter(fs);
  33. m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
  34. m_streamWriter.WriteLine("mcWindowsService: Service Started" + DateTime.Now.ToString() + "\n");
  35. m_streamWriter.Flush();
  36. m_streamWriter.Close();
  37. fs.Close();
  38. }
  39. protected override void OnStop()
  40. {
  41. FileStream fs = new FileStream(@"d:\mcWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write);
  42. StreamWriter m_streamWriter = new StreamWriter(fs);
  43. m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
  44. m_streamWriter.WriteLine(" mcWindowsService: Service Stopped " + DateTime.Now.ToString() + "\n");
  45. m_streamWriter.Flush();
  46. m_streamWriter.Close();
  47. fs.Close();
  48. }
  49. private static void TimedEvent(object source, ElapsedEventArgs e)         //运行期间执行
  50. {
  51. FileStream fs = new FileStream(@"d:\mcWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write);
  52. StreamWriter m_streamWriter = new StreamWriter(fs);
  53. m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
  54. m_streamWriter.WriteLine("  Running.11.. " + DateTime.Now.ToString() + "\n");
  55. m_streamWriter.Flush();
  56. m_streamWriter.Close();
  57. fs.Close();
  58. }
  59. }
  60. }

C#创建windows服务搭配定时器Timer使用实例(用代码做,截图版)的更多相关文章

  1. 解决windows 服务中定时器timer 定时偶尔失效 问题

    最近做个windows 服务,功能是:定时执行一个任务:自动登录到一个网站后,点击相关网面上的按钮button. 在处理的过程中发现定时器老是不定时的失效,失效时间没有规律. 由于刚开始处于测试阶段, ...

  2. vs 2010创建Windows服务定时timer程序

    vs 2010创建Windows服务定时timer程序: 版权声明:本文为搜集借鉴各类文章的原创文章,转载请注明出处:  http://www.cnblogs.com/2186009311CFF/p/ ...

  3. 使用Topshelf创建Windows 服务

    本文转载: http://www.cnblogs.com/aierong/archive/2012/05/28/2521409.html http://www.cnblogs.com/jys509/p ...

  4. 创建Windows服务(Windows Services)N种方式总结

    最近由于工作需要,写了一些windows服务程序,有一些经验,我现在总结写出来.目前我知道的创建创建Windows服务有3种方式:a.利用.net框架类ServiceBaseb.利用组件Topshel ...

  5. 使用 Topshelf 创建 Windows 服务

    Ø  前言 C# 创建 Windows 服务的方式有很多种,Topshelf 就是其中一种方式,而且使用起来比较简单.下面使用 Visual Studio Ultimate 2013 演示一下具体的使 ...

  6. (转)创建Windows服务(Windows Services)N种方式总结

    转自:http://www.cnblogs.com/aierong/archive/2012/05/28/2521409.html 最近由于工作需要,写了一些windows服务程序,有一些经验,我现在 ...

  7. 用C#创建Windows服务(Windows Services)

    用C#创建Windows服务(Windows Services) 学习:  第一步:创建服务框架 创建一个新的 Windows 服务项目,可以从Visual C# 工程中选取 Windows 服务(W ...

  8. 使用Topshelf创建Windows服务

    概述 Topshelf是创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通过5个步骤详细的 ...

  9. [Solution] Microsoft Windows 服务(2) 使用Topshelf创建Windows服务

    除了通过.net提供的windows服务模板外,Topshelf是创建Windows服务的另一种方法. 官网教程:http://docs.topshelf-project.com/en/latest/ ...

随机推荐

  1. delphi 怎么把image上的图片粘贴到剪切板上

    这个给你一个简单的代码,仅提供思路. uses Clipbrd; //引用单元 procedure TForm1.Button1Click(Sender: TObject); var Bmp: TBi ...

  2. Web前端开发工程师编程能力飞升之路

    [背景] 如果你是刚进入web前端研发领域,想试试这潭水有多深,看这篇文章吧:如果你是做了两三年web产品前端研发,迷茫找不着提高之路,看这篇文章吧:如果你是四五年的前端开发高手,没有难题能难得住你的 ...

  3. word添加页码

    问:在Word里面,分两栏插入页码,怎么使两栏都有页码? 可以通过插入域来实现分两栏后两栏都有页码.左边一栏页码为 当前页页码×2-1 :右边一栏页码为 当前页页码×2.在页眉页脚视图中,分别在左右两 ...

  4. Python 多线程学习(转)

    转自:http://www.cnblogs.com/slider/archive/2012/06/20/2556256.html 引言 对于 Python 来说,并不缺少并发选项,其标准库中包括了对线 ...

  5. FILETIME, SYSTEMTIME 与 time_t 相互转换

    FILETIME, SYSTEMTIME 与 time_t 相互转换 2009-08-24 15:37:14|  分类: 默认分类|举报|字号 订阅     //******************* ...

  6. RFID之UID

    1 Unique identifier (UID) The VICCs are uniquely identified by a 64 bits unique identifier (UID). Th ...

  7. 使用Maven将Hadoop2.2.0源码编译成Eclipse项目

    编译环境: OS:RHEL 6.3 x64 Maven:3.2.1 Eclipse:Juno SR2 Linux x64 libprotoc:2.5.0 JDK:1.7.0_51 x64 步骤: 1. ...

  8. openfire 最大连接数调优

    https://community.igniterealtime.org/thread/48064#224126 https://community.igniterealtime.org/thread ...

  9. 类似于QQ游戏百万人同时在线的服务器架构实现

    http://blog.csdn.net/sodme/article/details/213995 —————————————————————————————————————————————————— ...

  10. Clean Code第三章<函数>

    1.方法不要写太长,如果太长,抽取其中的逻辑到新的方法中 bad good 2.函数只做一件事 如果做了多件事,要在方法名里体现出来 3.每个函数一个抽象层级 4.函数名可以长一些,比长注释好 5.方 ...