C#在串口数据接收时随机挂起setmaterialname [英] C# hangs randomly in setmaterialname on data receive of serial port

查看:100
本文介绍了C#在串口数据接收时随机挂起setmaterialname的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我写了一个Mogre(Managed Ogre)C#应用程序。

应用程序随机挂起但是没有显示任何消息只是没有响应

我不确定,但我建议问题出在串口的Data_Receive处理程序中。

本节的代码如下所示。







Hi I have written a Mogre (Managed Ogre) C# application.
the application hangs randomly but don't show any message just " not responding"
i'm not sure but i suggest the problem is in Data_Receive handler of serial port.
the code of this section is presented in below.



<pre>       public void Open_Port()
        {
            try
            {

                Data_Comport = new SerialPort(PortName, 9600, Parity.None, 8, StopBits.One);
                Data_Comport.ReadTimeout = 5000;
                Data_Comport.WriteTimeout = 5000;
                Data_Comport.DataReceived += new SerialDataReceivedEventHandler(PortDataReceivedHandler);
                Data_Comport.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        public delegate void InvokeDelegate();
        private void PortDataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            if (Data_Comport.IsOpen)
            {
                //  try
                //   {
                //var line = Data_Comport.ReadLine();
                bool success = false;
                try
                {
                    success = int.TryParse(Data_Comport.ReadLine().ToString(), out myMogre.finger_id);
                }
                catch (InvalidOperationException ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                catch (TimeoutException ex)
                {
                    MessageBox.Show(ex.ToString());
                }

                if (success)
                {
                    //if (Data_Comport.ReadLine().Contains("-") && myMogre.finger_id == 1)
                    //    myMogre.finger_id = -myMogre.finger_id;
                    Data_Comport.DiscardInBuffer();
                    Data_Comport.DiscardOutBuffer();
                    myMogre.finger_id = myMogre.finger_id - 2;

                    try
                    {
                        if (myMogre.Verified)
                        {
                            for (int i = 0; i < 5; i++)
                            {
                                if (myMogre.finger_id + 2 == i)
                                {
                                    myMogre.Ok_Coin_Entity[i].SetMaterialName("coinsmat/Ok_coin_defok" + i.ToString());
                                }
                                else
                                {
                                    myMogre.Ok_Coin_Entity[i].SetMaterialName("coinsmat/Ok_coin_def" + i.ToString());
                                }
                            }

                            for (int i = 0; i < myMogre.N_of_Coins; i++)
                            {
                                if (myMogre.finger_id == myMogre.Random_Pos[i] && myMogre.Out_coins[i])
                                {
                                    if (!myMogre.long_lines_isselected[i])
                                    {
                                        myMogre.long_lines_Entities[i].SetMaterialName("Long/long_" + (myMogre.Random_Pos[i] + 2).ToString());
                                        myMogre.long_lines_isselected[i] = true;
                                    }
                                }
                                else
                                {
                                    myMogre.long_lines_Entities[i].SetMaterialName("Long/long" + (myMogre.Random_Pos[i] + 2).ToString());
                                }
                            }
                        }
                    }
                    catch (NullReferenceException ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
                /*                }
                                catch (System.IO.IOException error)
                                {
                                    return;
                                }
                                catch (System.InvalidOperationException error)
                                {
                                    return;
                                }*/
            }
        }





我有什么尝试过:



这个函数在Mogre线程的不同主题中可能很有用!



感谢你的建议!



What I have tried:

and it's may be useful that this function is in different thread of Mogre thread!

Thanks for your suggestions!

推荐答案

所以你的处理程序运行得很长 - 最好不要让它在UI线程上运行。你看到的是Windows看你的MessageLoop(Windows事件机制背后的好东西)的结果

如果没有处理更多的消息(事件),它会显示没有响应消息。

解决方案:您的整个datareceiver应该与UI无关,并且可以在单独的线程上完成所有工作。如果需要在UI中更新某些内容 - 为dataReceiver组件(类)创建一个Event并通过它通知UI。
So your handler is running to long - better don't let it run on UI-thread. What you see is the result of Windows watching your "MessageLoop" (the good old thing behind the Windows event-mechanism)
If no more messages (Events) are handled, it shows this "not responding" message.
Solution: your whole datareceiver should have nothing to do with UI and do all it's work on a separate thread. If something needs to be updated in the UI - create an Event for your dataReceiver component (class) and notify the UI through it.


这篇关于C#在串口数据接收时随机挂起setmaterialname的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆