随笔 - 435  文章 - 0  评论 - 111  阅读 - 62万 

一段有问题的代码,大概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 + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

            }



        }
复制代码

客户反映,第一次测试时,停不下来,之后就可以。

  

 

posted on   Gu  阅读(60)  评论(0编辑  收藏  举报
编辑推荐:
· 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爆火,是硬核还是营销?
点击右上角即可分享
微信分享提示