C # - Windows service creation and running
one using System; two using System. Collections. Generic; three using System. Linq; four using System. ServiceProcess; five using System. Text; six using System. Threading. Tasks; seven namespace WindowsService eight { nine static class Program ten { eleven /// <summary> twelve /// The main entry point for the application. thirteen /// </summary> fourteen static void Main() fifteen { sixteen ServiceBase[] ServicesToRun; seventeen ServicesToRun = new ServiceBase[] eighteen { nineteen new Service1() twenty }; twenty-one ServiceBase. Run(ServicesToRun); twenty-two } twenty-three } twenty-four }
one using System; two using System. Collections. Generic; three using System. IO; four using System. Linq; five using System. Text; six using System. Threading. Tasks; seven namespace WindowsService. Utils eight { nine public static class Common ten { eleven public static void WriteLogs( string content) twelve { thirteen string path = AppDomain. CurrentDomain. BaseDirectory; fourteen string LogName = System. Reflection. MethodBase. GetCurrentMethod(). DeclaringType. Namespace. Split( ' . ' )[ zero ]; fifteen string [] sArray = path. Split( new string [] { LogName }, StringSplitOptions. RemoveEmptyEntries); sixteen string aa = sArray[ zero ] + " \\ " + LogName + " Log\\ " ; seventeen path = aa; eighteen if (! string .IsNullOrEmpty(path)) nineteen { twenty if (! Directory. Exists(path)) twenty-one { twenty-two Directory. CreateDirectory(path); twenty-three } twenty-four path = path + " \\ " + DateTime. Now. ToString( " yyyy-MM-dd " ) + " .txt " ; // twenty-five if (! File. Exists(path)) twenty-six { twenty-seven FileStream fs = File. Create(path); twenty-eight fs. Close(); twenty-nine } thirty if (File.Exists(path)) thirty-one { thirty-two StreamWriter sw = new StreamWriter(path, true , System. Text. Encoding. Default); thirty-three sw. WriteLine(DateTime.Now.ToString( " yyyy-MM-dd HH:mm:ss " ) + " ---- " + content + " \r\n " ); thirty-four sw. Close(); thirty-five } thirty-six } thirty-seven } thirty-eight } thirty-nine }
one using System; two using System. Collections. Generic; three using System. Linq; four using System. Text; five using System. Threading. Tasks; six using WindowsService. Utils; seven namespace WindowsService eight { nine public class HandleService ten { eleven public static void ActionRun( ref bool isRun) twelve { thirteen try { fourteen isRun = true ; fifteen // Business code sixteen Common. WriteLogs( " This is a log " ); seventeen eighteen } nineteen catch (Exception ex) twenty { twenty-one Common. WriteLogs(ex.Message); twenty-two } twenty-three finally twenty-four { twenty-five isRun = false ; twenty-six } twenty-seven } twenty-eight } twenty-nine }
one using System; two using System. Collections. Generic; three using System. ComponentModel; four using System. Data; five using System. Diagnostics; six using System. Linq; seven using System. ServiceProcess; eight using System. Text; nine using System. Threading. Tasks; ten using WindowsService. Utils; eleven namespace WindowsService twelve { thirteen public partial class Service1 : ServiceBase fourteen { fifteen public Service1() sixteen { seventeen InitializeComponent(); eighteen } nineteen System. Timers. Timer _timer = new System. Timers. Timer(); twenty private bool isRun = false ; twenty-one protected override void OnStart( string [] args) twenty-two { twenty-three try twenty-four { twenty-five int _interval = five * one thousand ; twenty-six _timer. Interval = _interval; twenty-seven _timer. AutoReset = true ; twenty-eight _timer. Enabled = true ; twenty-nine _timer. Elapsed += new System. Timers. ElapsedEventHandler(ActionRun); thirty Common. WriteLogs( " Service started " ); thirty-one } thirty-two catch (Exception ex) thirty-three { thirty-four Common. WriteLogs(ex.Message); thirty-five } thirty-six } thirty-seven private void ActionRun( object sender, System. Timers. ElapsedEventArgs e) thirty-eight { thirty-nine try forty { forty-one if (! isRun) forty-two { forty-three HandleService. ActionRun( ref isRun); forty-four } forty-five } forty-six catch (Exception ex) forty-seven { forty-eight Common. WriteLogs( " Error: " + ex.Message); forty-nine } fifty } fifty-one protected override void OnStop() fifty-two { fifty-three _timer. AutoReset = false ; fifty-four _timer. Enabled = false ; fifty-five _timer. Stop(); fifty-six Common. WriteLogs( " Service stopped " ); fifty-seven } fifty-eight } fifty-nine } sixty
one Install. bat: two sc create MyWinService binPath= " %~dp0WindowsService.exe " start= auto three net start MyWinService four pause five
one Start.bat two net start MyWinService three pause four
one Stop. bat two net stop MyWinService three pause
one Uninstall.bat two net stop MyWinService three sc delete MyWinService binPath= " %~dp0JDWindowsService.exe " start= auto four pause
one Restart. bat two net stop MyWinService three net start MyWinService four pause