实战多线程交互

一段有问题的代码,大概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秒后线程发停止指令

 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 + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

            }



        }

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

  

原文地址:https://www.cnblogs.com/zitjubiz/p/14753370.html