功能说明: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. 12、NFC技术:读写NFC标签中的Uri数据

    功能实现,如下代码所示: 读写NFC标签的Uri 主Activity import cn.read.write.uri.library.UriRecord; import android.app.Ac ...

  2. 【转】linux之e2label命令

    转自:http://www.th7.cn/system/lin/201311/46743.shtml 前言 e2label命令,用于获取或设置ext2.ext3文件系统对应的分区的卷标. 卷标:简单来 ...

  3. JavaScript中的Function(函数)对象

    1.document.write(""); 输出语句 2.JS中的注释为// 3.传统的HTML文档顺序是:document->html->(head,body) 4. ...

  4. 拜托,这才是“Uber”的正确读法

    在美国,私家车主可以注册成为Uber司机,这对传统的出租车行业形成了很大的挑战,同时也让Uber始终处于舆论的风口浪尖. 7月14日,美国用车应用Uber正式宣布进入北京市场.在进入中国后,Uber选 ...

  5. Python线程

    原文出处: AstralWind 1. 线程基础 1.1. 线程状态 线程有5种状态,状态转换的过程如下图所示: 1.2. 线程同步(锁) 多线程的优势在于可以同时运行多个任务(至少感觉起来是这样). ...

  6. 【boost】BOOST_LOCAL_FUNCTION体验

    c++11里支持使用lambda在函数内定义本地嵌套函数,将一些算法的判断式定义为本地函数可以使代码更加清晰,同时声明和调用靠近也使得更容易维护.遗憾的是公司开发平台任然停留在vs2008,使用boo ...

  7. Hibernate中openSession() 与 getCurrentSession()的区别

    1 getCurrentSession创建的session会和绑定到当前线程,而openSession每次创建新的session. 2 getCurrentSession创建的线程会在事务回滚或事物提 ...

  8. eclipse gradle 自动打包

    直接在eclipse项目中建立一个文件,文件名为build.gradle.其实还可以用eclipse再项目上面右击,export->Android->Generate Gradle bui ...

  9. geeksforgeeks@ Sorting Elements of an Array by Frequency (Sort)

    http://www.practice.geeksforgeeks.org/problem-page.php?pid=493 Sorting Elements of an Array by Frequ ...

  10. dom 左右两侧得广告(兼容IE FF)

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...