Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.



C#
<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;
                                }*/
            }
        }


What I have tried:

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

Thanks for your suggestions!
Posted
Updated 28-Feb-18 3:05am
v2

1 solution

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.
 
Share this answer
 
Comments
Member 11588285 28-Feb-18 9:05am    
Hi.
Thanks for your answer! that solved my problem!
just moved part of code in the receiver method to another thread!
you saved my time!
thank you so much!
johannesnestler 2-Mar-18 5:24am    
nice to hear - you are welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900