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. [NOI2010][bzoj2006] 超级钢琴 [主席树/ST表+堆]

    题面: 传送门 思路: 首先容易想到用堆维护的O(n2logn)暴力 那么肯定就是在这个基础上套数据结构了[愉快] 然而我因为过于蒟蒻......只想得到主席树暴力***过去的方法 大概就是把前缀和算 ...

  2. linux删除大量文件

    1.建立一个空目录 mkdir -p /tmp/rsync_blank 2.确立需要清空的目标目录 /data/web/vip/htdocs/tuan 3.使用rsync同步删除(注意目录后面的“/” ...

  3. RSA DSA

    RSA https://blog.csdn.net/sunmenggmail/article/details/11994013 https://baike.baidu.com/item/RSA%E7% ...

  4. JavaScript Array 对象的方法,比如push和unshift

    https://www.runoob.com/jsref/jsref-obj-array.html js数组与字符串的相互转换 一.数组转字符串 需要将数组元素用某个字符连接成字符串,示例代码如下: ...

  5. 【16】vuex2.0 之 getter

    有的组件中获取到 store 中的state,  需要对进行加工才能使用,computed 属性中就需要写操作函数,如果有多个组件中都需要进行这个操作,那么在各个组件中都写相同的函数,那就非常麻烦,这 ...

  6. JavaScript (JS)基础:BOM 浅析 (含window对象相关基本方法、属性解析)

    ① window对象(Math方法也属于window对象): window对象是JavaScript中的顶级对象,所有定义在全局作用域中的变量.函数都会变成window对象的属性和方法,window对 ...

  7. pat 团体天梯 L3-003. 社交集群

    L3-003. 社交集群 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 在社交网络平台注册时,用户通常会输入自己的兴趣爱好, ...

  8. linux之函数

    17.1 基本的脚本函数 函数:是一个脚本代码块,可以为其命名并在代码中任何位置重用. 17.1.1 创建函数 有两种格式:name 是函数名 1) function name {          ...

  9. 多个电脑之间使用相同的ssh密钥

    首先我们给最先创建的密钥的电脑取名为OLD, 给后创建的密钥的电脑取名为NEW,在OLD上创建密钥,文件默认保存在 ~/.ssh/ 中: ssh-keygen –t rsa –C "your ...

  10. [Oracle] Redo&Undo梳理

    Oracle Redo&undo Oracle中的redo和undo是关键技术的核心, 诸如实例恢复, 介质恢复, DataGuard, 闪回机制等都是给予redo和undo的, 所以很有必要 ...