using System; using System.IO; using System.ServiceProcess; using System.Threading; using Vinno.FIS.Sonopost.Common; namespace Vinno.FIS.Sonopost.Service { public partial class SonopostService : ServiceBase { private string _sonopostPath; private ProcessKeepHelper _keepHelper; public SonopostService() { InitializeComponent(); } protected override void OnStart(string[] args) { try { Thread.Sleep(2000); try { ProcessHelper.FinishProcessByName("Vinno.FIS.Sonopost.Upgrade"); ProcessHelper.FinishProcessByName("Sonopost"); ProcessHelper.FinishProcessByName("Vinno.FIS.Sonopost.TRTCClient"); } catch (Exception e) { WriteLog(e.ToString()); } _sonopostPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Sonopost.exe"); var processId = ProcessHelper.StartProcess(_sonopostPath); _keepHelper = new ProcessKeepHelper(processId); _keepHelper.StartKeepProcess(() => { return ProcessHelper.StartProcess(_sonopostPath); }); } catch (Exception e) { WriteLog(e.ToString()); } } protected override void OnStop() { _keepHelper?.StopKeepProcess(); base.OnStop(); } private void WriteLog(string msg) { var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SonopostServiceLog.log"); File.AppendAllText(path, msg); } } }