1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.ServiceProcess; 5 using System.Text; 6 using System.Threading.Tasks; 7 namespace WindowsService 8 { 9 static class Program 10 { 11 /// <summary> 12 /// 應用程序的主入口點。 13 /// </summary> 14 static void Main() 15 { 16 ServiceBase[] ServicesToRun; 17 ServicesToRun = new ServiceBase[] 18 { 19 new Service1() 20 }; 21 ServiceBase.Run(ServicesToRun); 22 } 23 } 24 }
打開「Program.cs」,能夠看到服務啓動後,首先執行Service1。ide
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 namespace WindowsService.Utils 8 { 9 public static class Common 10 { 11 public static void WriteLogs(string content) 12 { 13 string path = AppDomain.CurrentDomain.BaseDirectory; 14 string LogName = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace.Split('.')[0]; 15 string[] sArray = path.Split(new string[] { LogName }, StringSplitOptions.RemoveEmptyEntries); 16 string aa = sArray[0] + "\\" + LogName + "Log\\"; 17 path = aa; 18 if (!string.IsNullOrEmpty(path)) 19 { 20 if (!Directory.Exists(path)) 21 { 22 Directory.CreateDirectory(path); 23 } 24 path = path + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";// 25 if (!File.Exists(path)) 26 { 27 FileStream fs = File.Create(path); 28 fs.Close(); 29 } 30 if (File.Exists(path)) 31 { 32 StreamWriter sw = new StreamWriter(path, true, System.Text.Encoding.Default); 33 sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "----" + content + "\r\n"); 34 sw.Close(); 35 } 36 } 37 } 38 } 39 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using WindowsService.Utils; 7 namespace WindowsService 8 { 9 public class HandleService 10 { 11 public static void ActionRun(ref bool isRun) 12 { 13 try { 14 isRun = true; 15 //業務代碼 16 Common.WriteLogs("這是一條日誌"); 17 18 } 19 catch (Exception ex) 20 { 21 Common.WriteLogs(ex.Message); 22 } 23 finally 24 { 25 isRun = false; 26 } 27 } 28 } 29 }
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Diagnostics; 6 using System.Linq; 7 using System.ServiceProcess; 8 using System.Text; 9 using System.Threading.Tasks; 10 using WindowsService.Utils; 11 namespace WindowsService 12 { 13 public partial class Service1 : ServiceBase 14 { 15 public Service1() 16 { 17 InitializeComponent(); 18 } 19 System.Timers.Timer _timer = new System.Timers.Timer(); 20 private bool isRun = false; 21 protected override void OnStart(string[] args) 22 { 23 try 24 { 25 int _interval = 5 * 1000; 26 _timer.Interval = _interval; 27 _timer.AutoReset = true; 28 _timer.Enabled = true; 29 _timer.Elapsed += new System.Timers.ElapsedEventHandler(ActionRun); 30 Common.WriteLogs("服務已啓動"); 31 } 32 catch (Exception ex) 33 { 34 Common.WriteLogs(ex.Message); 35 } 36 } 37 private void ActionRun(object sender, System.Timers.ElapsedEventArgs e) 38 { 39 try 40 { 41 if (!isRun) 42 { 43 HandleService.ActionRun(ref isRun); 44 } 45 } 46 catch (Exception ex) 47 { 48 Common.WriteLogs("Error:" + ex.Message); 49 } 50 } 51 protected override void OnStop() 52 { 53 _timer.AutoReset = false; 54 _timer.Enabled = false; 55 _timer.Stop(); 56 Common.WriteLogs("服務已中止"); 57 } 58 } 59 } 60
生成項目文件,所有生成成功後,就要開始服務的安裝和啓動spa
1 安裝.bat: 2 sc create MyWinService binPath= "%~dp0WindowsService.exe" start= auto 3 net start MyWinService 4 pause 5
1 啓動.bat 2 net start MyWinService 3 pause 4
1 中止.bat 2 net stop MyWinService 3 pause
1 卸載.bat 2 net stop MyWinService 3 sc delete MyWinService binPath= "%~dp0JDWindowsService.exe" start= auto 4 pause
1 重啓.bat 2 net stop MyWinService 3 net start MyWinService 4 pause