zoukankan      html  css  js  c++  java
  • C#系统服务定时执行(转)

    .新建项目 --》 Windows 服务

    2.Service1.cs代码

    [csharp] view plaincopyprint?

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Diagnostics;
    6. using System.ServiceProcess;
    7. using System.IO;
    8. using System.Text;
    9. using System.Timers;
    10. using System.Data.SqlClient;
    11. using System.Threading;
    12. namespace InnPoint
    13. {
    14. public partial class Service : ServiceBase
    15. {
    16. public Service()
    17. {
    18. InitializeComponent();
    19. }
    20. protected override void OnStart(string[] args)
    21. {
    22. EventLog.WriteEntry("我的服务启动");//在系统事件查看器里的应用程序事件里来源的描述
    23. writestr("服务启动");//自定义文本日志
    24. System.Timers.Timer t = new System.Timers.Timer();
    25. t.Interval = 1000;
    26. t.Elapsed += new System.Timers.ElapsedEventHandler(ChkSrv);//到达时间的时候执行事件;
    27. t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
    28. t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;
    29. }
    30. /// <summary>
    31. /// 定时检查,并执行方法
    32. /// </summary>
    33. /// <param name="source"></param>
    34. /// <param name="e"></param>
    35. public void ChkSrv(object source, System.Timers.ElapsedEventArgs e)
    36. {
    37. int intHour = e.SignalTime.Hour;
    38. int intMinute = e.SignalTime.Minute;
    39. int intSecond = e.SignalTime.Second;
    40. if (intHour == 13 && intMinute == 30 && intSecond == 00) ///定时设置,判断分时秒
    41. {
    42. try
    43. {
    44. System.Timers.Timer tt = (System.Timers.Timer)source;
    45. tt.Enabled = false;
    46. SetInnPoint();
    47. tt.Enabled = true;
    48. }
    49. catch (Exception err)
    50. {
    51. writestr(err.Message);
    52. }
    53. }
    54. }
    55. //我的方法
    56. public void SetInnPoint()
    57. {
    58. try
    59. {
    60. writestr("服务运行");
    61. //这里执行你的东西
    62. Thread.Sleep(10000);
    63. }
    64. catch (Exception err)
    65. {
    66. writestr(err.Message);
    67. }
    68. }
    69. ///在指定时间过后执行指定的表达式
    70. ///
    71. ///事件之间经过的时间(以毫秒为单位)
    72. ///要执行的表达式
    73. public static void SetTimeout(double interval, Action action)
    74. {
    75. System.Timers.Timer timer = new System.Timers.Timer(interval);
    76. timer.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e)
    77. {
    78. timer.Enabled = false;
    79. action();
    80. };
    81. timer.Enabled = true;
    82. }
    83. public void writestr(string readme)
    84. {
    85. //debug==================================================
    86. //StreamWriter dout = new StreamWriter(@"c:/" + System.DateTime.Now.ToString("yyyMMddHHmmss") + ".txt");
    87. StreamWriter dout = new StreamWriter(@"c:/" + "WServ_InnPointLog.txt", true);
    88. dout.Write("/r/n事件:" + readme + "/r/n操作时间:" + System.DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"));
    89. //debug==================================================
    90. dout.Close();
    91. }
    92. protected override void OnStop()
    93. {
    94. writestr("服务停止");
    95. EventLog.WriteEntry("我的服务停止");
    96. }
    97. }
    98. }
    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

  • 相关阅读:
    P31 整体更新或新增 PUT
    P30 整体更新/替换资源 PUT
    P29 自定义错误信息和错误报告
    python: openpyxl带格式复制excel
    Android控件与布局——基础控件RadioButton
    EditText 使用详解
    Linux内核内存检测工具KASAN
    ISP基础(10)-Gamma校正及其实现
    ISP基础(08)-动态范围压缩
    ISP基础(07)-自动曝光
  • 原文地址:https://www.cnblogs.com/dwfbenben/p/2600114.html
Copyright © 2011-2022 走看看