LYFZ_ErpService.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.Text;
  8. using System.Net;
  9. using System.Net.Sockets;
  10. using System.Threading;
  11. using System.Management;
  12. namespace LYFZ.WcfSystemService
  13. {
  14. /// <summary>
  15. /// 查看其各属性的含意是:
  16. /// Autolog 是否自动写入系统的日志文件
  17. /// CanHandlePowerEvent 服务时候接受电源事件
  18. /// CanPauseAndContinue 服务是否接受暂停或继续运行的请求
  19. /// CanShutdown 服务是否在运行它的计算机关闭时收到通知,以便能够调用 OnShutDown 过程
  20. /// CanStop 服务是否接受停止运行的请求
  21. /// ServiceName 服务名
  22. /// </summary>
  23. public partial class LYFZ_ErpWcfService : ServiceBase
  24. {
  25. private LYFZ.WindowsServiceHandling.MainService mainService = new LYFZ.WindowsServiceHandling.MainService();
  26. public LYFZ_ErpWcfService()
  27. {
  28. InitializeComponent();
  29. // SetTimerRunWork();//执行定时任务
  30. mainService.InitializeConstructor();
  31. }
  32. /// <summary>
  33. /// windows服务是不执行Timer控件的,解决办法
  34. /// </summary>
  35. protected void SetTimerRunWork()
  36. {
  37. System.Timers.Timer t = new System.Timers.Timer();
  38. t.Interval = 1000 * 10;
  39. t.Elapsed += new System.Timers.ElapsedEventHandler(RunWork);
  40. t.AutoReset = true;//设置是执行一次(false),还是一直执行(true);
  41. t.Enabled = true;//是否执行
  42. }
  43. public void RunWork(object source, System.Timers.ElapsedEventArgs e)
  44. {
  45. //要定时处理的事情
  46. //这样服务就可以定时执行任务了
  47. ServiceWriteLog("定时任务正在执行...");
  48. }
  49. /// <summary>
  50. /// 写服务日志
  51. /// </summary>
  52. /// <param name="txt"></param>
  53. protected void ServiceWriteLog(string txt)
  54. {
  55. }
  56. /// <summary>
  57. /// 开始
  58. /// </summary>
  59. /// <param name="args"></param>
  60. protected override void OnStart(string[] args)
  61. {
  62. // ServiceWriteLog("服务器启动成功.");
  63. mainService.OnStart(args);
  64. }
  65. /// <summary>
  66. /// 停止
  67. /// </summary>
  68. protected override void OnStop()
  69. {
  70. mainService.OnStop();
  71. // ServiceWriteLog("服务器已停止.");
  72. }
  73. /// <summary>
  74. /// 暂停
  75. /// </summary>
  76. protected override void OnPause()
  77. {
  78. mainService.OnPause();
  79. }
  80. /// <summary>
  81. /// 继续
  82. /// </summary>
  83. protected override void OnContinue()
  84. {
  85. mainService.OnContinue();
  86. }
  87. /// <summary>
  88. /// 计算机关闭时
  89. /// </summary>
  90. protected override void OnShutdown() {
  91. mainService.OnShutdown();
  92. }
  93. }
  94. }