protected override void OnStart(string[] args) { string msg = string.Format("启动目录:{0}\r\n启动文件:{1}\r\n程序启动……", AppDomain.CurrentDomain.BaseDirectory, Process.GetCurrentProcess().MainModule.FileName); Utils.Output(msg); // OnStart方法不允许有死循环,否则启动服务时会报错,改用Thread进行死循环 new Thread(TaskAutoRunService.Run) { IsBackground = true }.Start(); if (Common.GetBoolean("enableSocketListen")) { // 端口监听,处理管理程序的进程 var method = new SocketServer.OperationDelegate(TaskService.ServerOperation); new Thread(SocketServer.ListeningBySocket) { IsBackground = true }.Start(method); msg = " 开始监听端口:" + TaskService.ListenPort; Utils.Output(msg); } }
static void Main() { // 刚刚测试发现,如果error.exe直接抛出异常,比如 int.Parse("abc"); // 能在任务管理器里看到2个进程,持续几秒后2个都退出了 // Process.Start(@"F:\error.exe", ""); // return; AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; Process process = Process.GetCurrentProcess(); string msg = string.Format("启动目录:{0}\r\n启动文件:{1}\r\n\r\n 程序启动……", AppDomain.CurrentDomain.BaseDirectory, process.MainModule.FileName); if (IsRunning(process)) { msg += "\r\n应用程序已经在运行中。"; LogHelper.WriteCustom(msg, @"start\", false); Thread.Sleep(1000); Environment.Exit(1); } Console.WriteLine(msg); LogHelper.WriteCustom(msg, @"start\", false); // 轮询数据库,处理任务的线程 new Thread(TaskAutoRunService.Run) { IsBackground = true }.Start(); if (Common.GetBoolean("enableSocketListen")) { // 端口监听,处理管理程序的进程 var method = new SocketServer.OperationDelegate(TaskService.ServerOperation); new Thread(SocketServer.ListeningBySocket).Start(method); msg = " 开始监听端口:" + TaskService.ListenPort; Console.WriteLine(msg); LogHelper.WriteCustom(msg, @"start\", false); } }