USService.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.Threading;
  9. using System.Runtime.InteropServices;
  10. namespace US.Browser.Service
  11. {
  12. partial class USservice : ServiceBase
  13. {
  14. //线程对象
  15. Thread _TestForm = null;
  16. System.Timers.Timer timer = null;
  17. public USservice()
  18. {
  19. InitializeComponent();
  20. timer = new System.Timers.Timer();
  21. timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
  22. timer.Interval = 10000;
  23. }
  24. void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  25. {
  26. //定义 启动线程
  27. _TestForm = new Thread(new ThreadStart(StartListen));
  28. _TestForm.Start();
  29. }
  30. protected override void OnStart(string[] args)
  31. {
  32. // TODO: 在此处添加代码以启动服务。
  33. //判断当前系统是否为win7
  34. if (!(Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion").GetValue("ProductName").ToString().IndexOf("Windows 7") > -1))
  35. {
  36. //timer.Start();
  37. _TestForm = new Thread(new ThreadStart(StartListen));
  38. _TestForm.Start();
  39. }
  40. }
  41. protected override void OnStop()
  42. {
  43. // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
  44. timer.Stop();
  45. }
  46. public void StartListen()
  47. {
  48. ServerWindows.ServiceForm(EventLog);
  49. Message tmp = new Message();
  50. tmp.Left = System.Windows.Forms.Screen.GetWorkingArea(tmp).Width-tmp.Width;
  51. tmp.Top = System.Windows.Forms.Screen.GetWorkingArea(tmp).Height - tmp.Height;
  52. //显示窗体的代码
  53. System.Windows.Forms.Application.Run(tmp);
  54. while (true)
  55. {
  56. Thread.Sleep(100000);
  57. }
  58. }
  59. }
  60. }