/// <summary> /// 应用程序的主入口点。 /// </summary> public static void Start(EntranceParameter parameter) { Parameter = parameter; //初始化程序集自动搜索器 AssemblyAutoSearcher.Init(parameter.LoadAllPlugins); var args = parameter.StartupArguments; //如果没有带参数,则启动控制WinForm界面 if (args == null || args.Length == 0) { if (ProgramUtils.IsRuningOnWindows()) { WinFormLauncher.Launch(); return; } else { DebugLauncher.Launch(); return; } } String firstArg = args[0].ToLower(); switch (firstArg) { case "-debug": DebugLauncher.Launch(); break; case "-service": ServiceLauncher.Launch(); break; case "-install": new WinServiceInstaller().Install(); break; case "-uninstall": new WinServiceInstaller().Uninstall(); break; case "-start": new WinServiceInstaller().Start(); break; case "-stop": new WinServiceInstaller().Stop(); break; } }
public static void Launch() { WinService service = new WinService(); //如果是运行的Windows操作系统,则设置控制台标题 if (ProgramUtils.IsRuningOnWindows()) { Console.Title = ProgramUtils.GetProgramVersion(); } service.Start(null); while (true) { Console.Write(">"); var line = Console.ReadLine(); switch (line) { case "": break; case "help": Console.WriteLine("help\t显示帮助"); Console.WriteLine("cls\t清空屏幕"); Console.WriteLine("exit\t退出程序"); break; case "cls": Console.Clear(); break; case "exit": if (ProgramUtils.IsRuningOnWindows()) { Environment.Exit(0); } else { Process.GetCurrentProcess().Kill(); } return; default: Console.WriteLine("未知命令,键入help查看帮助"); break; } if (!ProgramUtils.IsRuningOnWindows()) { Console.WriteLine("Unknown command.you have to kill this process to terminal program."); Thread.CurrentThread.Join(); } } }
protected override void OnStart(string[] args) { if (!ProgramUtils.IsMonoRuntime() && ProgramUtils.IsRuningOnWindows() && Environment.Version < Version.Parse("4.0.30319.17929")) { throw new ApplicationException("需要安装4.5或更新版本的Microsoft .NET Framework才能运行此程序!"); } if (Entrance.Parameter.OnServiceStarting != null) { Entrance.Parameter.OnServiceStarting.Invoke(); } if (Entrance.Parameter.LoadAllPlugins) { pluginActivators = AppDomain.CurrentDomain.GetAssemblies() .Select(ass => ass.GetTypes().FirstOrDefault(t => !t.IsAbstract && !t.IsNotPublic && t.IsClass && typeof(IPluginActivator).IsAssignableFrom(t) )).Where(t => t != null) .Select(t => (IPluginActivator)Activator.CreateInstance(t)).ToArray(); //启动所有插件 foreach (var activator in pluginActivators) { activator.Start(); } } //启动所有服务 foreach (var service in ServiceManager.Instance.GetItems()) { Console.Write($"服务[{service.Name}]"); HunterUtils.TryHunt(service, Entrance.Parameter.Properties); Console.Write($"->启动中"); service.Start(); Console.WriteLine($"->完成"); } if (Entrance.Parameter.OnServiceStarted != null) { Entrance.Parameter.OnServiceStarted.Invoke(); } }
public static void Launch() { if (ProgramUtils.IsRuningOnWindows()) { ProgramUtils.FreeConsole(); } Thread staThread = new Thread(obj => { Application.ThreadException += ThreadExceptionCallbackFun; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); MainForm form = new MainForm(); Application.Run(form); }); staThread.SetApartmentState(ApartmentState.STA); staThread.Start(); }