CatchLotteryDataService.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. namespace CB.WService.CatchLotteryData
  8. {
  9. public partial class CatchLotteryDataService : ServiceBase
  10. {
  11. public CatchLotteryDataService()
  12. {
  13. InitializeComponent();
  14. }
  15. protected override void OnStart(string[] args)
  16. {
  17. var config = CB.LotteryCatchData.Config.CatchDataConfigs.GetConfig();
  18. if (null == config || null == config.List)
  19. {
  20. CB.Common.FileUtil.WriteLog("配置文件无效");
  21. return;
  22. }
  23. timer1.Interval = config.Interval;
  24. timer1.Enabled = true;
  25. }
  26. protected override void OnStop()
  27. {
  28. timer1.Enabled = false;
  29. }
  30. private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  31. {
  32. timer1.Enabled = false;
  33. DateTime startTime = DateTime.Now;
  34. var config = CB.LotteryCatchData.Config.CatchDataConfigs.GetConfig();
  35. if (null == config || null == config.List || 0 >= config.List.Count)
  36. {
  37. CB.Common.FileUtil.WriteLog("配置文件无效");
  38. timer1.Enabled = false;
  39. return;
  40. }
  41. foreach (var item in config.List)
  42. {
  43. try
  44. {
  45. CB.LotteryCatchData.CatchData.CatchLotteryData(item);
  46. }
  47. catch (Exception ex)
  48. { CB.Common.FileUtil.WriteLog(string.Format(item.Name + "抓取异常:{0} \r\n", ex)); }
  49. }
  50. timer1.Enabled = true;
  51. }
  52. }
  53. }