using HH.WCS.JingyuNongfu.api; using HH.WCS.JingyuNongfu.core; using HH.WCS.JingyuNongfu.device; using HH.WCS.JingyuNongfu.util; using Microsoft.Owin.Hosting; using System; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using Topshelf; using Monitor = HH.WCS.JingyuNongfu.core.Monitor; namespace HH.WCS.JingyuNongfu { internal class Program { static void Main(string[] args) { Settings.Init(); DisbleQuickEditMode(); //3.0 开启服务 var rc = HostFactory.Run(x => { x.Service(s => { s.ConstructUsing(name => new WorkThread()); s.WhenStarted(tc => tc.Start()); s.WhenStopped(tc => tc.Stop()); }); x.RunAsLocalSystem(); //x.RunAsLocalService(); x.SetDescription("hh"); x.SetDisplayName("hh.wms"); x.SetServiceName("hh.wms"); }); var exitCode = (int)Convert.ChangeType(rc, rc.GetTypeCode()); Environment.ExitCode = exitCode; } #region 关闭控制台 快速编辑模式、插入模式 const int STD_INPUT_HANDLE = -10; const uint ENABLE_QUICK_EDIT_MODE = 0x0040; const uint ENABLE_INSERT_MODE = 0x0020; [DllImport("kernel32.dll", SetLastError = true)] internal static extern IntPtr GetStdHandle(int hConsoleHandle); [DllImport("kernel32.dll", SetLastError = true)] internal static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint mode); [DllImport("kernel32.dll", SetLastError = true)] internal static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint mode); public static void DisbleQuickEditMode() { IntPtr hStdin = GetStdHandle(STD_INPUT_HANDLE); uint mode; GetConsoleMode(hStdin, out mode); mode &= ~ENABLE_QUICK_EDIT_MODE;//移除快速编辑模式 mode &= ~ENABLE_INSERT_MODE; //移除插入模式 SetConsoleMode(hStdin, mode); } #endregion private static void Startup() { Console.WriteLine("Startup ApiController"); Task.Run(() => { //var url = "http://192.168.1.51:8801";//{SettingHelper.port} var url = "http://+:8801"; using (WebApp.Start(url)) { Console.WriteLine("Running on {0}", url); Console.ReadLine(); while (true) { Thread.Sleep(1000); } } }); } private static void StartTcp() { var host = Dns.GetHostEntry(Dns.GetHostName()); foreach (var ip in host.AddressList) { if (ip.AddressFamily == AddressFamily.InterNetwork) { Console.WriteLine($"ip= {ip.ToString()}"); new TcpServer(ip.ToString()); } } } public class WorkThread { public void Start() { //1.0 开启api Startup(); //2.0 开启tcp StartTcp(); List tasks = new List(); //添加任务推送线程 tasks.Add(GetTask(TaskCore.Dispatch)); //添加自定义线程 tasks.Add(GetTask(Monitor.PPAuto)); tasks.Add(GetTask(Monitor.CPAuto)); //Task.WaitAll(tasks.ToArray()); } public void Stop() { Console.WriteLine("work stopped"); } private Task GetTask(Action action) { var task = Task.Run(() => { while (true) { try { action(); } catch (Exception ex) { LogHelper.Error(ex.Message, ex); } Thread.Sleep(3000); } }); return task; } } } }