一段有问题的代码,大概20%的概率,StartMotor的指令一直没有停下来
//线程一直循环,等待消息 while (true) { if (bStartSend) { LogInfo("bStartSend=true"); StartMotor(ref ErrMsg, ref recvMsg, his.strCurrentCmd); } System.Threading.Thread.Sleep(100); Application.DoEvents(); }
线程一直执行,没有收到停止信号,把主线程的代码也跳过了??? 应该是线程出现了异常,但异常没有被主线程Catch到
在线程里面用TryCatch,但是也没有Catch到异常
后来用bgWorkers.CancelAsync();来终止线程
----------------------------------------------------------该成线程里面来发停止指令----------------------------------------------
和线程的时间交互, 比如让线程发命令5秒,在5秒内,主线程读仪表读数,确认读到读数了。5秒后线程发停止指令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | static ManualResetEvent mr = new ManualResetEvent( false ); //连续发指令指定时间(比如5秒) public void StartMotorAtPeriod( int Millsec) { try { int BeginTime = timeGetTime(); string ErrMsg = "" ; string recvMsg = "" ; cmdCnt = 0; Task.Run(() => { try { while (timeGetTime() - BeginTime < Millsec) { LogInfo( "StartMotorAtPeriod,cmdCnt=" + cmdCnt); StartMotor( ref ErrMsg, ref recvMsg, this .strCurrentCmd); cmdCnt++; ////reset /set this work !!!!! mr.Reset(); System.Threading.Thread.Sleep(100); mr.Set(); throw new Exception( "exxxxxxx" ); } } catch (Exception te) { LogInfo(te.Message); } StopMotor(recvMsg); LogInfo( "StartMotorAtPeriod,Finish==========================" ); }); } catch (Exception ex) { if (ex.InnerException!= null ) LogInfo(ex.Message + ex.InnerException.Message); else LogInfo(ex.Message); } } |
//----------------------------------------还是有异常,跳过vb的函数--------------------------------------------------------
之前写过一个版本,是线程发命令结束后再去读仪表,不会出现问题。 改成线程发命令期间读仪表就会10%的机会有问题。区别在于线程和主界面都会写同一个日志文件。
改成线程写一个单独的日志,就不会出问题了。
改了一个版本,一直发,直到有结束信号
public void StartMotorWaitStopSignal(int MaxMillsec=30000, bool StopWhenFinish = false) { try { bStartSend = true; int BeginTime = timeGetTime(); string ErrMsg = ""; string recvMsg = ""; cmdCnt = 0; Task.Run(() => { while (timeGetTime() - BeginTime < MaxMillsec) { try { if (bStartSend) { LogInfo("StartMotorWaitStopSignal,cmdCnt=" + cmdCnt); StartMotor(ref ErrMsg, ref recvMsg, this.strCurrentCmd); cmdCnt++; } else { break; } ////reset /set this work !!!!! mr.Reset(); System.Threading.Thread.Sleep(100); mr.Set(); } catch (Exception te) { LogInfo(te.Message + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); } } if (StopWhenFinish) { for (int i = 0; i < 2; i++) { StopMotor(ref recvMsg); } } LogInfo("StartMotorWaitStopSignal,Finish=================================="); }); } catch (Exception ex) { if (ex.InnerException != null) LogInfo(ex.Message + ex.InnerException.Message + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); else LogInfo(ex.Message + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); } }
客户反映,第一次测试时,停不下来,之后就可以。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?