C#创建windows服务搭配定时器Timer使用实例(用代码做,截图版)

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

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

1.建立空白项目 

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

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

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

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

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

服务创建完成! 

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

6
服务类源代码():

C#代码  

  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使用实例(用代码做,截图版),布布扣,bubuko.com

时间: 2024-08-05 11:11:04

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

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

转自:http://www.cnblogs.com/aierong/archive/2012/05/28/2521409.html 最近由于工作需要,写了一些windows服务程序,有一些经验,我现在总结写出来.目前我知道的创建创建Windows服务有3种方式:a.利用.net框架类ServiceBaseb.利用组件Topshelfc.利用小工具instsrv和srvany 下面我利用这3种方式,分别做一个windows服务程序,程序功能就是每隔5秒往程序目录下记录日志: a.利用.net框架类

使用Topshelf创建Windows服务

本方式特点:代码简单,开源组件,Windows服务可运行多个实例 Topshelf是一个开源的跨平台的服务框架,支持Windows和Mono,只需要几行代码就可以构建一个很方便使用的服务. 官方网站:http://topshelf-project.com 第1步:引用程序集TopShelf.dll和log4net.dll 第2步:创建一个服务类MyClass,里面包含两个方法Start和Stop,还包含一个定时器Timer,每隔5秒往文本文件中写入字符 public class MyClass

使用Topshelf创建Windows 服务

本文转载:http://www.cnblogs.com/aierong/archive/2012/05/28/2521409.html b.利用组件Topshelf 本方式特点:代码简单,开源组件,Windows服务可运行多个实例 Topshelf是一个开源的跨平台的服务框架,支持Windows和Mono,只需要几行代码就可以构建一个很方便使用的服务. 官方网站:http://topshelf-project.com 第1步:引用程序集TopShelf.dll和log4net.dll 第2步:创

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

最近由于工作需要,写了一些windows服务程序,有一些经验,我现在总结写出来.目前我知道的创建创建Windows服务有3种方式:a.利用.net框架类ServiceBaseb.利用组件Topshelfc.利用小工具instsrv和srvany 下面我利用这3种方式,分别做一个windows服务程序,程序功能就是每隔5秒往程序目录下记录日志: a.利用.net框架类ServiceBase 本方式特点:简单,兼容性好 通过继承.net框架类ServiceBase实现 第1步: 新建一个Window

杂记2:VS2013创建Windows服务实现自动发送邮件

这篇随笔里,我将介绍如何用VS2013开发Windows服务项目,实现的功能是定时发送电子邮件. 开发环境:VS2013,SQL Server2008,采用C#语言开发 步骤一:创建Windows服务项目 首先,有人提问VS2013找不到创建Windows服务项目的选项,答案是在“Windows 桌面”目录下: 步骤二:重命名服务,添加Timer组件 重命名默认创建的Service1服务,比如MyMailService:然后在设计界面添加Timer组件. 这里要注意,VS工具箱默认提供的是Sys

Topshelf创建Windows服务

使用Topshelf创建Windows服务 概述 Topshelf是创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通过5个步骤详细的介绍使用使用Topshelf创建Windows 服务.Topshelf是一个开源的跨平台的宿主服务框架,支持Windows和Mono,只需要几行代码就可以构建一个很方便使用的服务宿主. 引用安装 1.官网:http://topshelf-project.c

使用Topshelf 5步创建Windows 服务 z

使用Topshelf创建Windows 服务简要的介绍了创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通过5个步骤详细的介绍使用使用Topshelf创建Windows 服务.Topshelf是一个开源的跨平台的宿主服务框架,支持Windows和Mono,只需要几行代码就可以构建一个很方便使用的服务宿主. 1.Topshelf的代码托管在http://topshelf-project.c

创建Windows服务简单流程

1.首先打开VS2010(或者其他版本),创建Windows服务项目 2.创建完成后切换到代码视图,代码中默认有OnStart和OnStop方法执行服务开启和服务停止执行的操作,下面代码是详细解释: 注意选择的是系统时间,不是winform中的时间. using System; using System.IO; usingSystem.ServiceProcess; using System.Text; usingSystem.Timers; namespaceTestService { pub

C#创建Windows服务与安装-图解

1.创建windows服务项目 2.右键点击Service1.cs,查看代码, 用于编写操作逻辑代码 3.代码中OnStart用于执行服务事件 public partial class Service1 : ServiceBase { string logFilePath = ""; LogHelper logHelper; WendyWuBll bll = new WendyWuBll(); public Service1() { logFilePath = Configuratio