Question DataGridView App Freezing/Hanging

FahriSnl

Member
Joined
Nov 22, 2021
Messages
5
Programming Experience
3-5
Hello everyone. I have a problem. I am getting datas from serial port than writing them in datagridview tool. If i add rows manually by a button there is no problem but if i write them in serial port recive function when datas reach edge of display app crashes. Do anyone know what is the problem?

Here code piece and error:

C#:
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{

    label1.Text = "";
    // 01 04 00 07 00 01 80 0B
    byte[] a = { 0x5A, 0xB2, 0xC3 };

    //Thread.Sleep(500);
    byte[] receivedData = new byte[5];
    serialPort1.Read(receivedData, 0, 5);

    byte crc = 0;
    crc = (byte)(receivedData[0] + receivedData[1] + receivedData[2] + receivedData[3]);


    label8.Text = crc.ToString();
    label9.Text = receivedData[4].ToString();

    if ((receivedData[3] % 25 == 0)&&(crc==receivedData[4]))
    {
        table.Rows.Add(receivedData[0], receivedData[1], receivedData[2], receivedData[3], receivedData[4]);
    }
    label6.Text = data1;


    int index = 0;

    foreach (byte b in receivedData)
    {
        label1.Text += DecimalToHexadecimal(b);
        // label1.Text += string.Format("{0:x}", b);
        if (index % 2 == 0)
            label1.Text += " ";
        index++;
    }


Works fine:
works.PNG



App Crashes:
error.PNG
 
Last edited by a moderator:
A crash is an unhanlded exception. The first thing to do is to look at the exception details, which will tell you where it was thrown and what the issue was.
 
A crash is an unhanlded exception. The first thing to do is to look at the exception details, which will tell you where it was thrown and what the issue was.
I dont know how to do that. I have searched and seems like need writing a code for that. Isn't there a more automatic way 😅😅
 
If you're running the application in the debugger then it will dis-play the Exception Assistant window when it crashes. That window provides all the information about the exception. If it only happens in the deployed application then add a try/catch block to the code to actually catch and log the exception.
 
Moved to Win Forms
 
Definitely need the exception details from the OP. Just scanning the code, the only thing that jumps out at me is that there could be a read time out exception thrown trying to read from the serial port if there is less than the 5 bytes that the code is trying to get.
 
Definitely need the exception details from the OP. Just scanning the code, the only thing that jumps out at me is that there could be a read time out exception thrown trying to read from the serial port if there is less than the 5 bytes that the code is trying to get.

I tried to get exeption but the probşem is code not sees it as an error. It just freezes. Here my code:
exception:
 try
                {
                    // some code
                    table.Rows.Add(receivedData[0], receivedData[1], receivedData[2], receivedData[3], receivedData[4]);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Seri Port Bağlantısı Yapılmadı! \n" + ex.Message
                        , "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

Its never catches. Or maybe Iam using try on wrong part of code?
 
Don't understand how the code could work at all, since DataReceived is raised on secondary thread no UI should be changed directly from that handler, use Control.Invoke for that as explained in Remarks in documentation.
 
I'm going to update your thread title to be more accurate...

Okay, so it's not a application crashing problem. It's a application freezing problem. Finding the source of that is different from trying to track down an exception. When app freezes, go into the Debug and tell it that you want to stop all threads. Then you start looking at the various threads to see which one is stuck. I suspect that that it's your serial1.Read() which is blocking waiting for more data to come in.
 
I wonder if someone is being naughty and set this property to false:

You mean like this 😅

CheckForIllegalCrossThreadCalls:
 private void Form1_Load(object sender, EventArgs e)
        {
            if (System.IO.Ports.SerialPort.GetPortNames().Length != 0)
            {
                foreach (var portName in System.IO.Ports.SerialPort.GetPortNames())
                {
                    //comboBoxPorts.Items.Add(portName);
                    fancy_ComboBox1.Items.Add(portName);
                }
                fancy_ComboBox1.SelectedIndex = 0;
            }
            Control.CheckForIllegalCrossThreadCalls = false;

            dataGridView1.DataSource = table;

        }

I did this because program wasn't allow me to change label's text in serial ports function. I saw people do this and i just did it.
 
You mean like this 😅
Yes, exactly that. You might do that for a quick and dirty test but even then I would frown on it. NEVER do it in real code.
 
How can I give permission to program to manupulate other components variables in their own class.

That's not the question you need answered. It's got nothing to do with permissions or classes. It's a simple fact of having to operate on a control in the thread that created it. In a WinForms app, the main thread is referred to as the UI thread for a reason, i.e. it is the thread that UI operations are almost always executed on. There are a number of ways that you can marshal a method call to the UI thread but, in a form, it is generally done by calling the Invoke method of the control you want to affect or the form itself. There are many examples of this on the web so I won't go into too much detail but a simple example would be changing this:
C#:
label6.Text = data1;
to this:
C#:
label6.Invoke(() => label6.Text = data1);
 
Back
Top Bottom