public static void StartService(string[] args, WorkerProcessInterface pgm) { try { if (Environment.UserInteractive) { Thread thKeyboard = new Thread(new ParameterizedThreadStart(KeyboardThread)); thKeyboard.Start(pgm); pgm.MainService(args); // Force Console.ReadLine() to exit by forcing in an [ENTER] key const int VK_RETURN = 0x0D; const int WM_KEYDOWN = 0x100; var hWnd = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle; PostMessage(hWnd, WM_KEYDOWN, VK_RETURN, 0); // Force Console.ReadLine() to exit by closing Stdin IntPtr stdin = GetStdHandle(StdHandle.Stdin); CloseHandle(stdin); } else { ServiceBase.Run(new ServiceBase[] { new Service1(pgm) }); } } catch (Exception ex) { pgm.LogInfo(ex.Message); } }
//=================== static void KeyboardThread(object obj) { WorkerProcessInterface pgm = (WorkerProcessInterface)obj; pgm.LogInfo("KeyboardThread Starting..."); Thread.Sleep(1000); try { while (pgm.IsAlive()) { string cmd = Console.ReadLine(); if (!pgm.IsAlive()) { break; } List <string> args = Service.ParseLine(cmd); if (args != null) { pgm.OnCommand(args); } } } catch (Exception e) { pgm.LogInfo("KeyboardThread: " + e.Message); } pgm.LogInfo("KeyboardThread Stopped"); }
public Service1(WorkerProcessInterface pgm) { //InitializeComponent(); this.pgm = pgm; }