123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
-
- using log4net;
- using SCADA.CommonLib;
- using SCADA.CommonLib.Helper;
- using SCADA.CommonLib.Service;
- using System;
- using System.Runtime.InteropServices;
- using System.Threading;
- using Topshelf;
- namespace SCADA_DAQ.Service
- {
- class ProgramMain
- {
- static void Main(string[] args)
- {
- ILog log = SCADA.CommonLib.LoggerHelper.Logger.CreateLogger(typeof(ProgramMain));
- AppDomain.CurrentDomain.UnhandledException += (s, e) =>
- {
- log.Error(e);
- };
- DisableConsoleCloseAction();
- Env.ParameterInit(Runtime.Instance);
- HostFactory.Run(x =>
- {
- var cfg = x.Service<Runtime>(
- s =>
- {
- s.ConstructUsing(name => Runtime.Instance);
- s.WhenStarted(t => t.Start());
- s.WhenPaused(t => t.Paused());
- s.WhenContinued(t => t.Resume());
- s.WhenStopped(t => t.Stop());
- });
- //x.UseLog4Net("log4net.config");
- x.SetDescription("SCADA 采集");
- x.SetDisplayName($"SCADA 采集 {ApplicationHelper.GetAppVersion()}");
- x.SetInstanceName("SCADA_Service");
- x.StartAutomatically(); // Automatic (Delayed) -- only available on .NET 4.0 or later
- x.RunAsLocalSystem();
- x.EnableServiceRecovery((e) =>
- {
- e.RestartService(0);//第一次失败执行
- e.RestartService(0);//第二次失败执行
- e.RestartService(0);//后续失败执行
- e.OnCrashOnly();
- e.SetResetPeriod(1);
- });
- x.OnException(ex =>
- {
- log.Error(ex); //异常处理
- })
- ;
- });
- }
- [DllImport("user32.dll")]
- private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
- [DllImport("user32.dll")]
- private static extern IntPtr GetSystemMenu(IntPtr hWnd, IntPtr bRevert);
- [DllImport("user32.dll")]
- private static extern IntPtr RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);
- private static void DisableConsoleCloseAction()
- {
- bool flag = true;
- while (flag)
- {
- Console.Title = _ConsoleTitle;
- IntPtr hWnd = FindWindow(null, _ConsoleTitle);
- IntPtr systemMenu = GetSystemMenu(hWnd, IntPtr.Zero);
- uint uPosition = 61536U;
- if (RemoveMenu(systemMenu, uPosition, 0U).ToInt32() != 0)
- {
- Console.WriteLine("success to disable close button.");
- flag = false;
- }
- else
- {
- Console.WriteLine("try to disable close button...");
- Thread.Sleep(1000);
- }
- }
- }
- private const string _ConsoleTitle = "采集服务正在运行,请不要关闭此窗口!(PLEASE Don't Close This Window!)";
- }
- public class Runtime : IApp
- {
- private string _sessionid;
- private Runtime()
- {
- }
- private static Runtime _instance;
- /// <summary>
- ///
- /// </summary>
- public static Runtime Instance { get => _instance ?? (_instance = new Runtime()); private set { _instance = value; } }
- public ThemeConfig ThemeConfig { get; }
- public SystemConfig SystemConfig { get; }
- public RuntimeInfo RuntimeInfo { get; set; }
- public IServiceHost ServiceHost { get; set; }
- public void Start()
- {
- RuntimeInfo.ProductId = "";
- RuntimeInfo.SessionId = _sessionid ?? (_sessionid = Guid.NewGuid().ToString());
- Console.WriteLine($"App Start {DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}");
- if (string.IsNullOrEmpty(SystemConfig.WebServer))
- {
- SystemConfig.WebServer = "www.imaodou.com.cn";
- }
- if (string.IsNullOrEmpty(SystemConfig.ProductTitle))
- {
- SystemConfig.ProductTitle = RuntimeInfo.ProductId;
- }
- if (string.IsNullOrEmpty(SystemConfig.AuthorInfo))
- {
- SystemConfig.AuthorInfo = "";
- }
- if (string.IsNullOrEmpty(SystemConfig.DefaultUser))
- {
- SystemConfig.DefaultUser = "Operator";
- }
- var host = SCADA_DAQ.ServiceHost.Instance;
- host.Start(this);
- ServiceHost = host;
- }
- public void Stop()
- {
- ServiceHost.Stop();
- }
- public void Paused()
- {
- }
- public void ReLoadLicense()
- {
- }
- public void Resume()
- {
- }
- public void Restart(bool runAsAdmin)
- {
- }
- public void Restart()
- {
- }
- public void ShowLongToast(object message, ControlStyle toastType = ControlStyle.Default)
- {
- }
- public void ShowShortToast(object message, ControlStyle toastType = ControlStyle.Default)
- {
- }
- public void ShowToast(object message, ControlStyle toastType = ControlStyle.Default, int showTime = 2000)
- {
- }
- }
- }
|