/// <summary> /// 线程内部调用,退出线程 /// </summary> /// <param name="wec"></param> /// <param name="info"></param> /// <param name="param"></param> protected void ExitWork(WorkExitCode wec, string info, object param = null) { //SendMsg2Outter(info); switch (wec) { case WorkExitCode.Command: //ChangeWorkStatus(JFWorkStatus.CommandExit); break; case WorkExitCode.Error: //ChangeWorkStatus(JFWorkStatus.ErrorExit); break; case WorkExitCode.Exception: //ChangeWorkStatus(JFWorkStatus.ExceptionExit); break; case WorkExitCode.Normal: //ChangeWorkStatus(JFWorkStatus.NormalEnd); break; } throw new JFWorkExitException(wec, info, param); }
public JFWorkExitException(WorkExitCode exitCode, string info, object param) : base() { ExitCode = exitCode; ExitInfo = info; ExitParam = param; }
protected virtual void ThreadFunc() { WorkExitCode exitCode = WorkExitCode.Normal; long cmdWaited = CommandUnknown; try { cmdEvent.WaitOne(); if (command != CommandStart) { ExitWork(WorkExitCode.Exception, "WorkThread receive first command is not CommandStart,command = " + command); } RespCmd(JFWorkCmdResult.Success); ChangeWorkStatus(JFWorkStatus.Running); PrepareWhenWorkStart(); while (true) { CheckCmd(CycleMilliseconds < 0?-1: CycleMilliseconds); RunLoopInWork(); } } catch (JFWorkExitException wee) //工作线程退出流程 { //Monitor.Exit(workStatusLocker); exitCode = wee.ExitCode; SendMsg2Outter("任务即将退出:" + exitCode + " 信息:" + wee.ExitInfo); } catch (Exception ex) { exitCode = WorkExitCode.Exception; SendMsg2Outter("任务发生未知的程序异常:" + ex.Message); ChangeWorkStatus(JFWorkStatus.ExceptionExit); } finally { try { OnStop(); } catch (JFWorkExitException eeStop) { switch (eeStop.ExitCode) { case WorkExitCode.Command: //此时不会再接受指令 ; // ChangeWorkStatus(JFWorkStatus.CommandExit); break; case WorkExitCode.Normal: ; // SendMsg2Outter("任务清理完成,退出运行"); break; case WorkExitCode.Error: SendMsg2Outter("OnStop() 发生错误:" + eeStop.Message); break; case WorkExitCode.Exception: SendMsg2Outter("OnStop() 发生异常:" + eeStop.Message); break; } } catch (Exception exOnStop) { SendMsg2Outter("OnStop() 发生程序异常:" + exOnStop.Message); } SendMsg2Outter("任务开始退出前清理"); try { CleanupWhenWorkExit(); SendMsg2Outter("任务清理完成,退出运行"); } catch (JFWorkExitException exCleanWEE) { switch (exCleanWEE.ExitCode) { case WorkExitCode.Command: //此时不会再接受指令 ; // ChangeWorkStatus(JFWorkStatus.CommandExit); break; case WorkExitCode.Normal: SendMsg2Outter("任务清理完成,退出运行"); break; case WorkExitCode.Error: SendMsg2Outter("任务清理发生错误:" + exCleanWEE.Message); break; case WorkExitCode.Exception: SendMsg2Outter("任务清理发生异常:" + exCleanWEE.Message); break; } } catch (Exception exCleanup) { SendMsg2Outter("任务清理发生未定义程序异常:" + exCleanup.Message); } switch (exitCode) { case WorkExitCode.Command: ChangeWorkStatus(JFWorkStatus.CommandExit); break; case WorkExitCode.Error: ChangeWorkStatus(JFWorkStatus.ErrorExit); break; case WorkExitCode.Exception: ChangeWorkStatus(JFWorkStatus.ExceptionExit); break; case WorkExitCode.Normal: ChangeWorkStatus(JFWorkStatus.NormalEnd); break; } } }