1.新建项目 --》 Windows 服务 2.Service1.cs代码

using System;  

using System.Collections.Generic;  

using System.ComponentModel;  

using System.Data;  

using System.Diagnostics;  

using System.ServiceProcess;  

using System.IO;  

using System.Text;  

using System.Timers;  

using System.Data.SqlClient;  

using System.Threading;  

namespace InnPoint  

{  

    public partial class Service : ServiceBase  

    {  

        public Service()  

        {  

            InitializeComponent();  

        }  

        protected override void OnStart(string[] args)  

        {  

            EventLog.WriteEntry("我的服务启动");//在系统事件查看器里的应用程序事件里来源的描述  

            writestr("服务启动");//自定义文本日志  

            System.Timers.Timer t = new System.Timers.Timer();  

            t.Interval = 1000;  

            t.Elapsed += new System.Timers.ElapsedEventHandler(ChkSrv);//到达时间的时候执行事件;   

            t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);   

            t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;   

        }  

        /// <summary>  

        /// 定时检查,并执行方法  

        /// </summary>  

        /// <param name="source"></param>  

        /// <param name="e"></param>  

        public void ChkSrv(object source, System.Timers.ElapsedEventArgs e)  

        {  

            int intHour = e.SignalTime.Hour;  

            int intMinute = e.SignalTime.Minute;  

            int intSecond = e.SignalTime.Second;  

             

            if (intHour == 13 && intMinute == 30 && intSecond == 00) ///定时设置,判断分时秒  

            {  

                try  

                {  

                    System.Timers.Timer tt = (System.Timers.Timer)source;  

                    tt.Enabled = false;  

                    SetInnPoint();  

                    tt.Enabled = true;  

                }  

                catch (Exception err)  

                {  

                    writestr(err.Message);  

                }  

            }  

        }  

        //我的方法  

        public void SetInnPoint()  

        {  

            try  

            {  

                writestr("服务运行");  

                //这里执行你的东西  

                Thread.Sleep(10000);  

            }  

            catch (Exception err)  

            {  

                writestr(err.Message);  

            }  

        }  

        ///在指定时间过后执行指定的表达式  

        ///  

        ///事件之间经过的时间(以毫秒为单位)  

        ///要执行的表达式  

        public static void SetTimeout(double interval, Action action)  

        {  

            System.Timers.Timer timer = new System.Timers.Timer(interval);  

            timer.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e)  

            {  

                timer.Enabled = false;  

                action();  

            };  

            timer.Enabled = true;  

        }  

  

        public void writestr(string readme)  

        {  

            //debug==================================================  

            //StreamWriter dout = new StreamWriter(@"c:/" + System.DateTime.Now.ToString("yyyMMddHHmmss") + ".txt");  

            StreamWriter dout = new StreamWriter(@"c:/" + "WServ_InnPointLog.txt", true);  

            dout.Write("/r/n事件:" + readme + "/r/n操作时间:" + System.DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"));  

            //debug==================================================  

            dout.Close();  

        }  

        protected override void OnStop()  

        {  

            writestr("服务停止");  

            EventLog.WriteEntry("我的服务停止");  

        }  

    }  

}  

3.在Service1.cs设计页面右键添加安装程序

4.ProjectInstaller.cs设计页面中

   serviceInstaller1属性中设置:

Description(系统服务的描述)

DisplayName (系统服务中显示的名称)

ServiceName(系统事件查看器里的应用程序事件中来源名称)

   serviceProcessInstaller1属性设置:

      Account 下拉设置成 LocalSystem

5.在.net Framework的安装目录可以找到InstallUtil.exe,可以复制出来放在你的服务生成的exe一起

6.安装服务setup.bat批处理文件内容:InstallUtil Service1.exe ,安装好后可以在系统服务中找到你设置的服务名称然后可以启动这个服务

7.卸载服务UnInstall.bat批处理文件内容:InstallUtil -u Service1.exe

转自:http://blog.csdn.net/windxxf/article/details/6014538

.net 定时执行 windows 服务的更多相关文章

  1. windows 服务实现定时任务调度(Quartz.Net)

    我们通常在一些情况下需要软件具有一个自动执行某些任务的功能,但是又不希望直接启动软件,或者每次都要手动的来启动软件,这时我们可可以考虑到windows服务了. 首先创建一个windows服务项目(详细 ...

  2. linux centos7 定时执行服务监控脚本

    2021-08-25 1. 需求 在服务挂掉之后我们要怎么做才能保证服务在短时间内开启?可以编写脚本监控服务的状态,在服务挂掉后及时将其开启,并定时执行该脚本. 2. 脚本编写 思路:平常我们可以通过 ...

  3. Centos7 利用crontab定时执行任务及配置方法

    crond是什么? crond 和crontab是不可分割的.crontab是一个命令,常见于Unix和类Unix的操作系统之中,用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于 ...

  4. linux下使用crontab定时执行脚本

    使用crontab定时执行脚本 cron服务是一个定时执行的服务,可以通过crontab 命令添加或者编辑需要定时执行的任务: crontab –e : 修改 crontab 文件,如果文件不存在会自 ...

  5. 使用 Topshelf 结合 Quartz.NET 创建 Windows 服务

    Ø  前言 之前一篇文章已经介绍了,如何使用 Topshelf 创建 Windows 服务.当时提到还缺少一个任务调度框架,就是 Quartz.NET.而本文就展开对 Quartz.NET 的研究,以 ...

  6. 快速搭建多线程Windows服务解决方案

    一.引言 在软件开发过程中windows服务有的时候非常有用,用于同步数据,发送邮件,宿主WF引擎服务等,但是快速搭建一个好用多线程进行多任务处理的程序往往是一个项目必须考虑的问题.自己在项目中也经常 ...

  7. C#创建windows服务并定时执行

    一.创建window服务 1.新建项目-->选择Windows服务.默认生成文件包括Program.cs,Service1.cs 2.在Service1.cs添加如下代码: System.Tim ...

  8. .net 开发定时执行的windows服务

    环境:win7+vs2010+Oracle11g+office2010(64位操作系统) 需求:开发定时执行的windows服务从数据库中查询数据下载到指定地址Excel中 一.添加新建项目——win ...

  9. C# 编写windows服务及服务的安装、启动、删除、定时执行任务

    一.编写windows服务 1.VS2017  - 创建服务Myservice 2.创建好项目之后 --- >> 双击 Service1.cs  ---- >>  出现一个设计 ...

随机推荐

  1. inline-block元素垂直对齐

    多个inline-block元素使用vertical-align:middle无法对齐,必须有个height:100%的子元素才行,通常使用伪元素.另一种方法是添加line-height:normal ...

  2. 论文笔记:《OverFeat: Integrated Recognition, Localization and Detection using Convolutional Networks DeepLearning 》

    一.Abstract综述 训练出一个CNN可以同时实现分类,定位和检测..,三个任务共用同一个CNN网络,只是在pool5之后有所不同 二.分类 这里CNN的结构是对ALEXNET做了一些改进,具体的 ...

  3. Ubuntu安装opencv with cuda

    Ubuntu安装opencv with cuda 为了运行dense flow真是折腾啊,下面网址是教程 http://blog.aicry.com/ubuntu-14-04-install-open ...

  4. 分布式文件系统之Glusterfs

    1.环境规划如下 centos7.4   三个节点一块 sdb  3G大小的测试硬盘 2.Glusterfs  卷的类型比较多,这里我们测试最常用的一种   Distributed Replicate ...

  5. 简单解决 ATL:CString WTL:CString 冲突

    //stdafx.h #include <atlbase.h> #include <atlstr.h> #include <atlapp.h> extern CAp ...

  6. 粟粟的书架(bzoj 1926)

    Description 幸福幼儿园 B29 班的粟粟是一个聪明机灵.乖巧可爱的小朋友,她的爱好是画画和读书,尤其喜欢 Thomas H. Co rmen 的文章.粟粟家中有一个 R行C 列的巨型书架, ...

  7. 【linux命令】lscpu,/etc/cpuinfo详解

    lscpu  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 i2000:~ # lscpu Architecture:        ...

  8. codeforce div2 426 D. The Bakery

    D. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...

  9. 自动从网站上面下载文件 .NET把网站图片保存到本地

    原文发布时间为:2009-11-19 -- 来源于本人的百度文章 [由搬家工具导入] 自动从网站上面下载文件 .NET把网站图片保存到本地简单范例:这个范例是把百度联盟这个logo保存到根目录下面,当 ...

  10. VUE 使用中踩过的坑

    vue如今可谓是一匹黑马,github star数已居第一位!前端开发对于vue的使用已经越来越多,它的优点就不做介绍了,本篇是我对vue使用过程中以及对一些社区朋友提问我的问题中做的一些总结,帮助大 ...